I teach C ++. I want to figure out how I / O works in the console. Already realized that if you type in cin words through space / tabulation, it reads them through this given delimiter and prints to cout . For example, I enter "hello man" , - displays "helloman" . This is normal, everything is clear. I also found out that CTRL+Z halts the process and means the symbol of the end files when reading. I press ctrl+Z (I don’t enter symbols before) and the process ends.

Question 1 : why, if I enter, for example, "hello ^Z" (I entered ^Z with the keyboard shortcut instead of manually first ^, then Z), then hello first appears in the output stream, and nothing appears behind it, only if you press enter, a question mark appears in the box! (what is it all about?)

Question 2 : If then cin reads characters through delimiters, then why didn’t it consider hello first, then ^ Z and, according to the command, did not complete the process, reading the end-of-file character, as it is called in the Stroustrup book. Explain, please.

 int main() { string current; while(cin>>current){ cout << current; } } 

    1 answer 1

    This is a feature of cmd.exe .

    cmd.exe sends text to the input stream line by line, and Ctrl + Z closes the input stream only if no other characters have been entered before it.
    You can verify this by rewriting the program as follows:

     #include <iostream> #include <string> int main() { std::string s; while (std::cin >> s) for (int b : s) std::cout << std::hex << b << ' '; } 

    If something was entered before ^Z , then it is interpreted as \x1a ( replacement character ), and everything entered in this line will be ignored:

     > test.exe 1^Z23 4 31 1a 34 

    At the same time, the default std::istream does not consider \1a a word delimiter.

    If nothing is entered before ^Z , then the stream is closed, and subsequent characters in the string are ignored:

     > test.exe 123 31 32 33 ^Z456 > 
    • Well, but why then in windows this keyboard shortcut is not enough that it does not immediately appear if you write it through a space with another word in the output stream, as I wrote hello hello => hellohello, but hello ^Z => hello (press enter ) => Question in the box - Muller
    • @Muller ^Z closes the stream immediately, but the istream learns that the stream is closed when it reads the entire stream. - Abyx
    • @Muller and as I already wrote, you cannot "write" ^Z This is not a symbol. You probably write some \x2 or \x3 . - Abyx
    • That's exactly what he read the entire file and how he should know about closing the stream, having read to ^ Z, but he does not close it, but continues to receive data - Muller
    • I just use the combination ctrl-z and the symbol ^Z itself appears on the screen - Muller