I haven't done any C coding in many years, so am reading through my Kernighan & Ritchie book again, to fully re-aquaint myself with it. One exercise is to write a program using getchar() to get keyboard input, and print it to the screen/stdout, replacing all tab, space and backspace characters with their escape sequence representations. I can't seem to get the following code to behave as expected.
Firstly, getchar() seems to be echoing input to the screen without me specifically outputting it with putchar() or printf().
The escape sequence representations don't display until I press ENTER.
The test for backspace doesn't seem to work at all.
I have mingw-w64 installed on win7. The above behaviour has got me scratching my head, and I'd really like to understand what's happening, before I move on. Any help greatly appreciated, and appologies in advance if I've made a basic mistake or assumption.
I haven't done any C coding in many years, so am reading through my Kernighan & Ritchie book again, to fully re-aquaint myself with it. One exercise is to write a program using getchar() to get keyboard input, and print it to the screen/stdout, replacing all tab, space and backspace characters with their escape sequence representations. I can't seem to get the following code to behave as expected.
Firstly, getchar() seems to be echoing input to the screen without me specifically outputting it with putchar() or printf().
The escape sequence representations don't display until I press ENTER.
The test for backspace doesn't seem to work at all.
I have mingw-w64 installed on win7. The above behaviour has got me scratching my head, and I'd really like to understand what's happening, before I move on. Any help greatly appreciated, and appologies in advance if I've made a basic mistake or assumption.
Thanks in advance,
Chris
include <stdio.h></stdio.h>
int main()
{
int c;
while((c = getchar()) != EOF)
{
if (c == '\t')
printf("\\t");
else if (c == '\b')
printf("\\b");
else if (c == '\\')
printf("\\\\");
else
putchar(c);
}
return 0;
Thanks for your reply, Kai. I had already seen the page you linked to, when I was looking for an answer. It doesn't mention anything about echoing input to the output, though. I think I've just misunderstood how getchar() operates. I thought it was purely an input function, with putchar() being the output equivalent.
Still wondering why backspace doesn't seem to be recognized by the code, though. Any idea?
Thanks
Chris
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I think you want the non standard conio _getch which is unbuffered and no echo (_getche echos). getchar I think buffers until it sees the cr/lf. Meaning that the backspaces are buffered out.
Note that in a while loop you'll have to do your own processing for EOF because those functions don't return an error code. (e.g. when you see the character ctrl-z, you should quit break the loop, ctrl-c should quit the program).
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I haven't done any C coding in many years, so am reading through my Kernighan & Ritchie book again, to fully re-aquaint myself with it. One exercise is to write a program using getchar() to get keyboard input, and print it to the screen/stdout, replacing all tab, space and backspace characters with their escape sequence representations. I can't seem to get the following code to behave as expected.
Firstly, getchar() seems to be echoing input to the screen without me specifically outputting it with putchar() or printf().
The escape sequence representations don't display until I press ENTER.
The test for backspace doesn't seem to work at all.
I have mingw-w64 installed on win7. The above behaviour has got me scratching my head, and I'd really like to understand what's happening, before I move on. Any help greatly appreciated, and appologies in advance if I've made a basic mistake or assumption.
Thanks in advance,
Chris
Hello,
\o/ finally after 10 years someone found the bug in msvcrt's getchar()
implementation. We are all in your debt for this ....
Sorry for kidding here a bit, but of some interesting might be
tutorial about use of getchar(). See for example
https://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm
with sample.
Hopr this link will help you to answer your question,
Kai
Am Di., 11. Sep. 2018 um 18:10 Uhr schrieb Chris Chadwick
big10p@users.sourceforge.net:
Thanks for your reply, Kai. I had already seen the page you linked to, when I was looking for an answer. It doesn't mention anything about echoing input to the output, though. I think I've just misunderstood how getchar() operates. I thought it was purely an input function, with putchar() being the output equivalent.
Still wondering why backspace doesn't seem to be recognized by the code, though. Any idea?
Thanks
Chris
I think you want the non standard conio
_getch
which is unbuffered and no echo (_getche
echos). getchar I think buffers until it sees the cr/lf. Meaning that the backspaces are buffered out.Note that in a while loop you'll have to do your own processing for EOF because those functions don't return an error code. (e.g. when you see the character ctrl-z, you should quit break the loop, ctrl-c should quit the program).
Thanks, Doug - that makes sense. I'll check out those functions in conio.
Cheers
Chris