EOF behavior followed by other values

* Note: I use windows, so EOF for me is ctrl + Z.

For a while, I noticed that the EOF input behaves differently in isolation than when using another input. For example, ^Z (the EOF command for windows on the command line) and a^Z seem to cause different behavior in the following code:

 #include <stdio.h> #define MAX 1000 int getline(char s[]); main() { int line; char arr[MAX]; while( (line = getline(arr)) != EOF) printf("%s",arr); system("Pause"); return 0; } int getline(char s[]) { int c, i = 0; while ((c = getchar()) != EOF && c != '\n') { s[i++] = c; } if (c == '\n') { s[i++] = c; } else return EOF; s[i] = '\0'; return 1; } 

If I type ^Z + at the command line, the program will confidently switch to system("Pause"); However, if I type abc^Z + enter, nothing will happen, as if EOF was ignored and the command '\n' never received. If at this moment I press the key again, it shows the following:

Eof weirdness

I have been doing this debugging and debugging this code and its small variants for more than an hour and can’t find anything wrong with that. Theoretically, if I type abc^Z + enter, I expect the input to be interpreted as abcEOF\n , which will give:

 s[0] = 'a' s[1] = 'b' s[2] = 'c' i = 3 when loop breaks from c = EOF if (c == '\n') skipped since c = EOF leads to else -> return EOF in main(), line = EOF since that is what the function getline returned while loop breaks because of above system("Pause"); follows 

Is there something wrong with my code that I am missing, or is there some kind of quirk for EOF or the command line that I should be aware of? I am pretty sure that this is not the only case where mixing ^Z with other values ​​caused unintended behavior.

+1
source share
2 answers

Do not think of CTRL-Z as the end-of-file character, in fact there is a real character with a code point of 26 (this is what you usually expect CTRL-Z ).

Instead, think of CTRL-Z as how your end device will indicate the end of the input stream to your program. If you were to read the real file on disk and contained CTRL-Z , it should continue (although this may not be the case in some implementations where, for example, you open it using r mode instead of rb ).

On Windows, this CTRL-Z transfer to the final stream operation occurs only when it appears at the beginning of the line, so you do not get EOF when you enter abc CTRL-Z .

When you enter CTRL-Z at a character position that is not the beginning of a line, it is treated as a real CTRL-Z , not a stream close operation. This is why you get this character β†’ , which is the printable character at point 26 of the code.

+4
source

This is normal getchar behavior ..

getchar () is a standard function and requires you to press * ENTER * to get input

Many compilers / platforms support custom getch (), which does not require input.

EOF is not a symbol. EOF is a macro that getchar () returns when it reaches the end of the input or encounters some error

0
source

Source: https://habr.com/ru/post/1200821/


All Articles