getchar / putchar - how does it really works ?
-
My task is to input individual characters , terminate the input with "space" or "ESC" and then retrieve / save the characters entered. getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed. putchar outputs ( to stdout) EACH character entered in do { ...} while loop - runs the loop. Questions 1. Is there a way to terminate getchar other than entering "enter "? 2. How does putchar "runs" in do { .. } while loop ignoring getchar? 3. How can I "retrieve" characters in WHAT buffer for additional processing AFTER the do{} while loop is terminated ?
char next; do { next = getchar(); // outputs each character as entered putchar(next); // outputs characters in loop } while (next != '\\n'); // terminates with enter key press / new line /n
-
My task is to input individual characters , terminate the input with "space" or "ESC" and then retrieve / save the characters entered. getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed. putchar outputs ( to stdout) EACH character entered in do { ...} while loop - runs the loop. Questions 1. Is there a way to terminate getchar other than entering "enter "? 2. How does putchar "runs" in do { .. } while loop ignoring getchar? 3. How can I "retrieve" characters in WHAT buffer for additional processing AFTER the do{} while loop is terminated ?
char next; do { next = getchar(); // outputs each character as entered putchar(next); // outputs characters in loop } while (next != '\\n'); // terminates with enter key press / new line /n
Vaclav_ wrote:
getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed.
No,
getchar
merely reads the next character entered at the terminal. If the terminal is in (normal) echo mode then it displays the character as typed. Theputchar
function merely displays a character, it has no knowledge of the loop or anything else. 1. Yes, you can terminate on anything, just inspect the character as entered and compare it to your terminator. 2. As above,putchar
does nothing beyond displaying a single character. 3. Save them somewhere. -
Vaclav_ wrote:
getchar and its variety waits in loop (?) and outputs to stdout until "enter" key is pressed.
No,
getchar
merely reads the next character entered at the terminal. If the terminal is in (normal) echo mode then it displays the character as typed. Theputchar
function merely displays a character, it has no knowledge of the loop or anything else. 1. Yes, you can terminate on anything, just inspect the character as entered and compare it to your terminator. 2. As above,putchar
does nothing beyond displaying a single character. 3. Save them somewhere.I am seeing this
char next; do { next = getchar(); // outputs each character as entered cout << "text entered " << endl; putchar(next); // outputs characters in loop } while (next != '\\n'); // terminates with enter key press / new line /n
qwerty
text entered
qtext entered
wtext entered
etext entered
rtext entered
ttext entered
ytext entered -
I am seeing this
char next; do { next = getchar(); // outputs each character as entered cout << "text entered " << endl; putchar(next); // outputs characters in loop } while (next != '\\n'); // terminates with enter key press / new line /n
qwerty
text entered
qtext entered
wtext entered
etext entered
rtext entered
ttext entered
ytext enteredyou need to turn off canonical mode on input. man termios, and look for ICANON. As a quick test without recompiling:
$ myprog
qwerty
text entered
qtext entered
wtext entered
etext entered
rtext entered
ttext entered
ytext entered$ stty -icanon
$ myprog
qtext entered
qwtext entered
wetext entered
ertext entered
rttext entered
tytext entered
y
text entered$ stty icanon
See: [FAQ - Cprogramming.com](https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392)
-
I am seeing this
char next; do { next = getchar(); // outputs each character as entered cout << "text entered " << endl; putchar(next); // outputs characters in loop } while (next != '\\n'); // terminates with enter key press / new line /n
qwerty
text entered
qtext entered
wtext entered
etext entered
rtext entered
ttext entered
ytext enteredIt seems that reading characters one at a time is no longer possible (on Windows). While
getchar
returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit] -
It seems that reading characters one at a time is no longer possible (on Windows). While
getchar
returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit]It has nothing to do with windows :-) Whoever writes the C/C++ library determines how it works, you are making a C/C++ standard call not a windows call and if they don't do the standard then it is hardly a fault of windows. On windows really the easy way is to hook the WM_CHAR and/or WM_SYSCHAR messages because the message queue is available even in console apps since windows XP service pack 2.
In vino veritas
-
It has nothing to do with windows :-) Whoever writes the C/C++ library determines how it works, you are making a C/C++ standard call not a windows call and if they don't do the standard then it is hardly a fault of windows. On windows really the easy way is to hook the WM_CHAR and/or WM_SYSCHAR messages because the message queue is available even in console apps since windows XP service pack 2.
In vino veritas
-
It seems that reading characters one at a time is no longer possible (on Windows). While
getchar
returns the characters one at a time, the system does not present them until after the enter key was pressed. So although you are trying to read single characters, they are still streamed internally. [edit] Same result on Ubuntu. [/edit]Richard MacCutchan wrote:
the system does not present them until after the enter key was pressed.
Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Richard MacCutchan wrote:
the system does not present them until after the enter key was pressed.
Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
Richard MacCutchan wrote:
the system does not present them until after the enter key was pressed.
Not quite true, or at least not complete: if there is something in the input buffer already, getchar will immediately return with the next char it finds in that buffer. It will only wait if the input buffer is empty. AFAIK this is not dependend on the OS.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
No it doesn't always it depends on the canonical mode of your input, this was covered above. The C standard doesn't cover how the underlying system is operating and it cant present what it doesn't yet have. Richard simply has an OS that is in canonical mode.
In vino veritas
-
No it doesn't always it depends on the canonical mode of your input, this was covered above. The C standard doesn't cover how the underlying system is operating and it cant present what it doesn't yet have. Richard simply has an OS that is in canonical mode.
In vino veritas
Well it always worked as I described for me. But I admit I haven't heard of the canonical mode before, so I'm going to trust you on this.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)