Reading from file in c++ ifstream

What the inFile >> S does is take in the file stream, which is the data in you file, and uses a space delimiter (breaks it up by whitespace) and puts the contents in the variable S.

For example:

If we had a file that had the follow contents

the dog went running

and we used inFile >> S with our file:

ifstream inFile("doginfo.txt")
string words;
while(inFile >> words) {
   cout << words << endl;
}

we will get the following output:

the
dog
went
running

The inFile >> S will continue to return true until there are no more items separated by whitespace.

Leave a Comment