Actual difference between end of line and end of file under windows?
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 wit开发者_运维知识库h '\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 tells getline
to 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 either Enter
, Control-J
, or Control-M
). On Windows, the end of a line in a file is marked with a CR
-LF
pair (\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-Z
denotes end-of-file; on Unix/Linux, it's typically control-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:
one
two
Let's say you're using C's getchar()
, getc()
, or fgetc()
function to read one character at a time. The values returned on successive calls will be:
'o', 'n', 'e', '\n', 't', 'w', 'o', '\n', EOF
Or, in numeric form (on a typical system):
111, 110, 101, 10, 116, 119, 111, 10, -1
Each '\n'
, or 10 (0x0a) is a new-line character read from the file. The final -1
is 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++'s std::cin >> s
or std::getline(std::cin, s)
, are built on top of this mechanism.
First "part"
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.
No, you're confusing formatted input operations with stream iterators. The following will use the formatted input operation (operator>>
) repeatedly until the end of file is reached because the "end iterator" represents the end of the stream.
std::vector<int> integers;
std::copy(
std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(integers));
If you use the following:
int i = 0;
std::cin >> i;
in an interactive shell (e.g. in console mode), std::cin
will block on user input which is acquired line by line. So, if no data (or only white space) is available, this operation will actually force the user to type a line of input and press the enter key.
However,
int i = 0;
int j = 0;
std::cin >> i >> j;
may block on one or two lines of input, depending on what the user types. In particular, if the user types
1<space>2<enter>
then the two input operations will be applies using the same line of input.
Second "part"
Considering the code snippet:
std::cin.getline(char_array_of_size_256 ,256);
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "artist is " << artist << endl;
If the line contains 255 or less lines of character data, std::cin.getline()
will consume the end-of-line character. Thus, the second line will consume all characters until the next line is completed. If you want to capture only the current line and ignore all characters past 256, I suggest you use something like:
std::cin.getline(char_array_of_size_256 ,256);
if (std::cin.gcount() == 256) {
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
cout << "artist is " << artist << endl;
On the second part:
When the linked answer said "read into a string", I guess they meant
std::string s;
std::getline(std::cin, s);
which always reads the entire line into the string s
(while setting s
to the proper size).
That way there is nothing left over from the input line to clean up.
精彩评论