cin >> and cin.ignore()

I read that the extraction operator stops extracting bytes from the buffer when it encounters '\n','\t',blanks and white spaces. When this happens, does it simply leave the '\n' in the buffer or does it remove it from the buffer before it stops?

I wonder the same thing about let's say std::cin.ignore(1000,'\n'). When it encounters the newline, does it leave this byte in this stream or does it extract it and "throw it away" before the function stops?

>> leaves the whitespace in the buffer.

ignore() will extract and remove the delim character.
std::cin.get() doesn't remove the '\n' just like >> right?
Correct. get() also leaves the delim character.
Thank you Zhuge :).
std::istream.get extracts one character, doesnt matter if white space or not.

std::ifstream file( "data.txt" )

char ch;
while ( file.get( ch ) ) std::cout << ch ;  // prints a copy of the file 
The .get() member function will take any character, regardless of what it is.

If you use .get() for a string or integer (or somthing else with more than 1 character), then Zhuge is right, whitespaces will be left.
std::istream.get() does not take integers or strings as arguments.

You can pass it a char*, in which case white space will extracted and added to the array. It wont extract the delimeter though.

@ Zerpent: Sometimes it is best to check yourself via a small program. There is also the reference here.


Last edited on
Topic archived. No new replies allowed.