Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Active reading [<https://en.wiktionary.org/wiki/up_to#Preposition>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

The stream operator >> is used to read formatted white space separated text.

int val1; stream >> val1; // reads a space separated int float val2; stream >> val2; // reads a space separated float std::string val3; stream >> val3; // reads a space separated word. 

Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (u ptoup to the null terminator, '\0', of the C-string).

If you want to read a whole line of text use std::getline() std::string line; std::getline(stream, line); 

But like most languages, you can loop reading the stream until it is finished.

std::string word; while(stream >> word) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Or the same thing one line at a time:

std::string line; while(std::getline(stream, line)) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file, but ignore new line.

Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).

The stream operator >> is used to read formatted white space separated text.

int val1; stream >> val1; // reads a space separated int float val2; stream >> val2; // reads a space separated float std::string val3; stream >> val3; // reads a space separated word. 

Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (u pto null terminator '\0' of the C-string).

If you want to read a whole line of text use std::getline() std::string line; std::getline(stream, line); 

But like most languages you can loop reading the stream until it is finished.

std::string word; while(stream >> word) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Or same thing one line at a time:

std::string line; while(std::getline(stream, line)) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file but ignore new line.

Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).

The stream operator >> is used to read formatted white space separated text.

int val1; stream >> val1; // reads a space separated int float val2; stream >> val2; // reads a space separated float std::string val3; stream >> val3; // reads a space separated word. 

Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (up to the null terminator, '\0', of the C-string).

If you want to read a whole line of text use std::getline() std::string line; std::getline(stream, line); 

But like most languages, you can loop reading the stream until it is finished.

std::string word; while(stream >> word) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Or the same thing one line at a time:

std::string line; while(std::getline(stream, line)) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file, but ignore new line.

Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).

Source Link
Loki Astari
  • 266.6k
  • 87
  • 344
  • 575

The stream operator >> is used to read formatted white space separated text.

int val1; stream >> val1; // reads a space separated int float val2; stream >> val2; // reads a space separated float std::string val3; stream >> val3; // reads a space separated word. 

Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (u pto null terminator '\0' of the C-string).

If you want to read a whole line of text use std::getline() std::string line; std::getline(stream, line); 

But like most languages you can loop reading the stream until it is finished.

std::string word; while(stream >> word) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Or same thing one line at a time:

std::string line; while(std::getline(stream, line)) { // Reads one word at a time until the EOF. std::cout << "Got a word (" << word << ")\n"; } 

Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file but ignore new line.

Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).