Avoid having to press Enter

Discussion in 'C Programming' started by Johan Svedberg, Oct 10, 2003.

  1. Hi!

    I read somewhere that it is impossible to write a program in ANSI-C
    which only reads one char from the keyboard without having to press
    Enter (i.e. [y/n]-questions). Is this true?

    --
    Johan Svedberg, , http://johan.svedberg.pp.se/
    Johan Svedberg, Oct 10, 2003
    #1
    1. Advertising

  2. Johan Svedberg <> scribbled the following:
    > Hi!


    > I read somewhere that it is impossible to write a program in ANSI-C
    > which only reads one char from the keyboard without having to press
    > Enter (i.e. [y/n]-questions). Is this true?


    That's only more-or-less true. You see, ANSI-C doesn't read anything
    from the keyboard at all. Your console does. It then feeds the data to
    ANSI-C via a text stream. ANSI-C can read whatever it wants from this
    stream, whenever it wants, regardless of whether you press Return or
    not.
    The thing is, however, your console usually doesn't put the data into
    the stream in the first place, until you press Return. Therefore, until
    you press Return, ANSI-C is blissfully ignorant to the existence of the
    data at all.
    Simply switch your console from "cooked" to "raw" mode and you're all
    set. How do you switch it? That's an OS question, I'm afraid, not an
    ANSI-C one.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "The question of copying music from the Internet is like a two-barreled sword."
    - Finnish rap artist Ezkimo
    Joona I Palaste, Oct 10, 2003
    #2
    1. Advertising

  3. Johan Svedberg

    pete Guest

    Joona I Palaste wrote:
    >
    > Johan Svedberg <> scribbled the following:
    > > Hi!

    >
    > > I read somewhere that it is impossible to write a program in ANSI-C
    > > which only reads one char from the keyboard without having to press
    > > Enter (i.e. [y/n]-questions). Is this true?

    >
    > That's only more-or-less true. You see, ANSI-C doesn't read anything
    > from the keyboard at all. Your console does.


    I'm looking at my computer, my monitor and my keyboard.
    I'm under the impression that my keyboard is the file
    associated with the standard input stream and that my monitor
    is the file associated with the standard output stream
    on my system.
    What's my console ?

    --
    pete
    pete, Oct 11, 2003
    #3
  4. pete <> scribbled the following:
    > Joona I Palaste wrote:
    >> Johan Svedberg <> scribbled the following:
    >> > Hi!

    >>
    >> > I read somewhere that it is impossible to write a program in ANSI-C
    >> > which only reads one char from the keyboard without having to press
    >> > Enter (i.e. [y/n]-questions). Is this true?

    >>
    >> That's only more-or-less true. You see, ANSI-C doesn't read anything
    >> from the keyboard at all. Your console does.


    > I'm looking at my computer, my monitor and my keyboard.
    > I'm under the impression that my keyboard is the file
    > associated with the standard input stream and that my monitor
    > is the file associated with the standard output stream
    > on my system.
    > What's my console ?


    Your console is the program or other software component responsible
    for reading from your keyboard and writing to your monitor. ANSI-C
    does not provide hardware APIs to the physical devices.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns
    Joona I Palaste, Oct 11, 2003
    #4
  5. Johan Svedberg

    pete Guest

    Joona I Palaste wrote:
    >
    > pete <> scribbled the following:
    > > Joona I Palaste wrote:


    > >> That's only more-or-less true.
    > >> You see, ANSI-C doesn't read anything
    > >> from the keyboard at all. Your console does.

    >
    > > I'm looking at my computer, my monitor and my keyboard.
    > > I'm under the impression that my keyboard is the file
    > > associated with the standard input stream and that my monitor
    > > is the file associated with the standard output stream
    > > on my system.
    > > What's my console ?

    >
    > Your console is the program or other software component responsible
    > for reading from your keyboard and writing to your monitor. ANSI-C
    > does not provide hardware APIs to the physical devices.


    I'm trying to understand how consoles
    fit into the C standard's description of the world.
    There's the computer and there are files.
    The computer has an operating system (assuming not freestanding).
    The flow of information between the computer and a file,
    is a stream.
    I guess that the console is part of the operating system,
    and that the stream flows through the console, to the file.

    --
    pete
    pete, Oct 15, 2003
    #5
  6. pete <> scribbled the following:
    > Joona I Palaste wrote:
    >> pete <> scribbled the following:
    >> > Joona I Palaste wrote:
    >> >> That's only more-or-less true.
    >> >> You see, ANSI-C doesn't read anything
    >> >> from the keyboard at all. Your console does.

    >>
    >> > I'm looking at my computer, my monitor and my keyboard.
    >> > I'm under the impression that my keyboard is the file
    >> > associated with the standard input stream and that my monitor
    >> > is the file associated with the standard output stream
    >> > on my system.
    >> > What's my console ?

    >>
    >> Your console is the program or other software component responsible
    >> for reading from your keyboard and writing to your monitor. ANSI-C
    >> does not provide hardware APIs to the physical devices.


    > I'm trying to understand how consoles
    > fit into the C standard's description of the world.
    > There's the computer and there are files.
    > The computer has an operating system (assuming not freestanding).
    > The flow of information between the computer and a file,
    > is a stream.
    > I guess that the console is part of the operating system,
    > and that the stream flows through the console, to the file.


    Consoles are part of the operating system, yes. But the only come into
    play when you are using the keyboard and the screen. There the console
    goes between the actual physical devices and the streams. When using
    files, the operating system connects the file directly to the streams.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "'It can be easily shown that' means 'I saw a proof of this once (which I didn't
    understand) which I can no longer remember'."
    - A maths teacher
    Joona I Palaste, Oct 15, 2003
    #6
  7. Johan Svedberg

    pete Guest

    Joona I Palaste wrote:
    >
    > pete <> scribbled the following:
    > > Joona I Palaste wrote:
    > >> pete <> scribbled the following:
    > >> > Joona I Palaste wrote:
    > >> >> That's only more-or-less true.
    > >> >> You see, ANSI-C doesn't read anything
    > >> >> from the keyboard at all. Your console does.
    > >>
    > >> > I'm looking at my computer, my monitor and my keyboard.
    > >> > I'm under the impression that my keyboard is the file
    > >> > associated with the standard input stream and that my monitor
    > >> > is the file associated with the standard output stream
    > >> > on my system.
    > >> > What's my console ?
    > >>
    > >> Your console is the program or other software component responsible
    > >> for reading from your keyboard and writing to your monitor. ANSI-C
    > >> does not provide hardware APIs to the physical devices.

    >
    > > I'm trying to understand how consoles
    > > fit into the C standard's description of the world.
    > > There's the computer and there are files.
    > > The computer has an operating system (assuming not freestanding).
    > > The flow of information between the computer and a file,
    > > is a stream.
    > > I guess that the console is part of the operating system,
    > > and that the stream flows through the console, to the file.

    >
    > Consoles are part of the operating system, yes. But the only come into
    > play when you are using the keyboard and the screen. There the console
    > goes between the actual physical devices and the streams. When using
    > files, the operating system connects the file directly to the streams.


    Thank you.

    --
    pete
    pete, Oct 15, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    570
    Natty Gur
    Jun 30, 2003
  2. Jie
    Replies:
    7
    Views:
    6,125
    Xavier Pacheco
    Oct 9, 2004
  3. ad
    Replies:
    4
    Views:
    828
    jasonkester
    Aug 26, 2005
  4. Roger23
    Replies:
    2
    Views:
    984
    Roger23
    Oct 12, 2006
  5. HopfZ
    Replies:
    0
    Views:
    537
    HopfZ
    Aug 28, 2006
Loading...

Share This Page