Copy algorithm with ifstream

The following code does not behave as I expected. Please help me understand how this works.

#include <algorithm> #include <iterator> #include <fstream> #include <vector> #include <string> using namespace std; struct user { string name; string age; string id; }; istream& operator>>(istream& is, user& s) { getline(is, s.name, ':'); getline(is, s.age, ':'); getline(is, s.id); return is; } int main(int argc, char* argv[]) { ifstream file("file.txt"); vector<user> vec; copy(istream_iterator<user>(file), istream_iterator<user>(), back_inserter(vec)); return 0; } 

My custom operator -> is called twice, but I expect it to be called only once, because the contents are:

John: forty: 21-5821-0

+6
source share
1 answer

In general, to read the entire file, you read until the read completes with an error. Then you know that something went wrong, or you got them all. In any case, you may not know that you have reached the end of the file until you read it. Since the first is successful, he should try a second time to find out if there is a second element. Psudocode for this

 while(in_stream >> object) { myvector.push_back(object); } 

Also note that this is an โ€œidiomaticโ€ way of reading values โ€‹โ€‹in the entire file. If you check eof , fail or bad , you are probably in error code.

However, your istream& operator>>(istream& is, user& s) function istream& operator>>(istream& is, user& s) great. The second time it is called, the first getline will fail, setting the stream to a bad state (eof), the other two getline also fail, and it will return the stream, and everything will work fine. Just remember that any or all of these variables can be completely nonsense because reading fails.

+3
source

Source: https://habr.com/ru/post/912090/


All Articles