This question has already been answered:

Good evening. Tell me why this code:

while (!file.eof()) { file >> p1.x >> p1.y; MyVector.push_back(p1); cout << p1.x << " " << p1.y<<endl; } 

With this information in the file:

 0 0 0 10 10 10 10 0 

Displays the following:

 0 0 0 10 10 10 10 0 10 0 

and accordingly the size of the vector will be equal to 5?

PS need to get around this moment

Reported as a duplicate member by VladD c ++ Nov 22 '17 at 6:05 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Do not do this: while (!file.eof())

    This check will work only after unsuccessful reading past the end of the file.

    And here you are

     file >> p1.x >> p1.y; MyVector.push_back(p1); 

    Good luck reading do not check ...

    Well, since @ixSci insists ... [Mentor tone] - should be like this:

     while (file >> p1.x >> p1.y) { MyVector.push_back(p1); cout << p1.x << " " << p1.y<<endl; } 
    • @ Harry, the read success is checked above in the code. Including file availability using #include <experimental/filesystem> and !is_regular_file(filename) - LokenGarvel '22
    • So you show the person how to correctly, what did the problem just describe? - ixSci
    • Well, if you know and understand everything - why ask? - Harry
    • @ixSci Man, as you see, rests with his legs and hands ... And then - I actually wrote - to check the success of reading - is this not enough? - Harry
    • one
      He just does not understand, hardly rests. - ixSci