I understand EOF and EOL but when I was reading this question (second part of answer) and i got my concepts broken :
Specially the para :
It won’t stop taking input until it finds the end of file(cin uses
stdin, which is treated very much like a file)
so i want to know when we do some thing like in c++ under windows :
std::cin>>int_var; , and we press enter , this end the input but according to reference link it should only stop taking input after hitting ctrl+z.
So i would love to know how std::*stream deal with EOF and EOL.
Second part:
please have a look at this example :
std::cin.getline(char_array_of_size_256 ,256); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cout << "artist is " << artist << endl;
If i remove std::cin.ignore() it simply stops taking input (which is known case) but when i keep it , it waits for a new input which is ended by '\n' . But it should simply clear up stream rather then waiting for any new input ending-up with ‘\n’.
Thanks for giving you time)
End-of-line and end-of-file are very different concepts.
End-of-line is really just another input character (or character sequence) that can appear anywhere in an input stream. If you’re reading input one character at a time from a text stream, end-of-line simply means that you’ll see a new-line (
'\n') character. Some input routines treat this character specially; for example, it tellsgetlineto stop reading. (Other routines treat' 'specially; there’s no fundamental difference.)Different operating systems use different conventions for marking the end of a line. On Linux and other Unix-like systems, the end of a line in a file is marked with a single ASCII linefeed (
LF, ‘\n’) character. When reading from a keyboard, both LF and CR are typically mapped to'\n'(try typing eitherEnter,Control-J, orControl-M). On Windows, the end of a line in a file is marked with aCR–LFpair (\r\n). The C and C++ I/O systems (or the lower-level software they operate on top of) map all these markers to a single'\n'character, so your program doesn’t have to worry about all the possible variations.End-of-file is not a character, it’s a condition that says there are no more characters available to be read. Different things can trigger this condition. When you’re reading from a disk file, it’s just the physical end of the file. When you’re reading from a keyboard on Windows,
control-Zdenotes end-of-file; on Unix/Linux, it’s typicallycontrol-D(though it can be configured differently).(You’ll usually have an end-of-line (character sequence) just before end-of-file, but not always; input can sometimes end in an unterminated line, on some systems.)
Different input routines have different ways of indicating that they’ve seen an end-of-file condition. Read the documentation for each one for the details.
As for
EOF, that’s a macro defined in<stdio.h>or<cstdio>. It expands to a negative integer constant (typically -1) that’s returned by some functions to indicate that they’ve reached an end-of-file condition.EDIT: For example, suppose you’re reading from a text file containing two lines:
Let’s say you’re using C’s
getchar(),getc(), orfgetc()function to read one character at a time. The values returned on successive calls will be:Or, in numeric form (on a typical system):
Each
'\n', or 10 (0x0a) is a new-line character read from the file. The final-1is the value of EOF; this isn’t a character, but an indication that there are no more characters to be read.Higher-level input routines, like C’s
fgets()and C++’sstd::cin >> sorstd::getline(std::cin, s), are built on top of this mechanism.