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.
source share