![]() |
Avoid having to press Enter
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, johan@svedberg.pp.se, http://johan.svedberg.pp.se/ |
Re: Avoid having to press Enter
Johan Svedberg <winkle@mao.acc.umu.se> 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 (palaste@cc.helsinki.fi) ---------------------------\ | 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 |
Re: Avoid having to press Enter
Joona I Palaste wrote:
> > Johan Svedberg <winkle@mao.acc.umu.se> 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 |
Re: Avoid having to press Enter
pete <pfiland@mindspring.com> scribbled the following:
> Joona I Palaste wrote: >> Johan Svedberg <winkle@mao.acc.umu.se> 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 (palaste@cc.helsinki.fi) ---------------------------\ | 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 |
Re: Avoid having to press Enter
Joona I Palaste wrote:
> > pete <pfiland@mindspring.com> 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 |
Re: Avoid having to press Enter
pete <pfiland@mindspring.com> scribbled the following:
> Joona I Palaste wrote: >> pete <pfiland@mindspring.com> 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 (palaste@cc.helsinki.fi) ------------- 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 |
Re: Avoid having to press Enter
Joona I Palaste wrote:
> > pete <pfiland@mindspring.com> scribbled the following: > > Joona I Palaste wrote: > >> pete <pfiland@mindspring.com> 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 |
| All times are GMT. The time now is 09:46 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.