I am trying to understand EOF and EOL, and how C++ iostream actually works.
While taking the the input through getchar() or getche() into a char variable, I found that if I write lines like:
char a;
a = getche(); // it returns char '\r' if pressed enter
a = getchar(); // it returns char '\n' if pressed enter
Why these values?
-
What actually makes C++ think that we have run out of input (i.e is it always
'\n'that makes C++ think that it’s at the end of its input?). -
While reading/writing a file that has some string sentences ending with
'\n'then what happens if lines end with aNULLcharacter, which also represents an end-of-line?
Could you explain all these briefly with examples?
Firstly,
getcheis a POSIX function fromconio.hthat is non-standard and deprecated in all major toolchains.It’s an unbuffered, non-formatted read operation. When your input stream uses
\r\nfor line endings (common on Windows), then you are reading that first character\r.When you then perform
getchar(), you’re getting the second character,\n. This is a C function, too.The rest of my answer will be about C++.
The buffered I/O functions tend to delimit reads by
\n, yes. There is a parameter tostd::getlinewhich allows you to change this delimiter:But this is just a delimiter. You may consider that it signifies “End Of Line”, but it’s certainly not “End Of File”.
Null characters don’t matter.
The only time null characters are a problem is in C-style
charbuffer strings with no accompanying length information. The only way to determine the string’s length becomes searching for the terminating null character (see:strlen), which is problematic if there are arbitrary other null characters scattered throughout the useful part of the data.If you’re passing around a pointer to a
chararray and its size as anint, then it can contain as many null characters as you like.When reading characters from a stream, in C or C++, the function you use tells you how many characters were read. So, even if some of them were null characters, it doesn’t matter. You can handle them as you see fit.
I didn’t quite understand this question, but I’ll wrap up my answer by briefly describing End Of File.
Historically, files had a physical character
\004(^D) that sat at the end of its contents and represented End Of File.Nowadays this physical character isn’t used in this manner, but the internals of the OS and File System will use varying mechanisms to inform your application that there is no more input. The C functions will return the macro
EOF, and the C++ objects have a state flag that you can check.The detail of precisely how this works is abstracted away from you; you shouldn’t have to care about it.
Interestingly, to end input in a Linux console, it’s still
^Dthat you press on the keyboard.I hope that this has helped you somewhat. Your question wasn’t particularly clear, but the above is intended to be a brief description of EOL and EOF in C++.
I can recommend these books and resources for further reading.