really easy c++: Does the operator >> in fstream remove newline characters after reading something?

c++, fstream

Solution

It leaves the delimiter in the input buffer, so the next character you read will be a new-line. Note, however, that most extractors will skip white space (which includes `new-line`) before anything they extract, so unless you do call something like `getline` this won't usually be visible.

Edit: to test on something like ideone, consider using a `stringstream`:

#include <sstream>
#include <iostream>

int main(){ 
    std::istringstream f("12\n13");
    int n;
    char c;
    f >> n;
    f.get(c); // get takes a reference, not a pointer. Don't take the address.

    std::cout << "'" << c << "'";
    return 0;
}

I wouldn't expect to see a difference between a `stringstream` and an `fstream` in something like this.

Problem

``` // file.in 12 13 // main.cpp fstream f("file.in", ios::in); int n; char c; f >> n; f.get(&c); ``` After extracting the number 12, what is the next character? Is it newline or '1'? If I call getline instread of get, do I get an empty line or '13'?

Original source

Related problems