I am trying to read from a file with a list of numbers and add them to a vector. Initially, this file will be empty, but after the user has entered more data(from other parts of my code) it will contain the list of numbers.
My problem is that when i ouput the vector to the screen, tlist[0] is rubbish, or if i initialize data it will be what i initialized it to. If i read in an integer before the while loop, it takes care of the problem...only the first time however. Once there is data in the file, it gets screwed up.
Any thoughts? Am I going about this the right way?
Code:ifstream filein("List.txt"); vector <int> tlist; int counter = 0, data; while( ! filein.eof() ) { filein >> data; tlist.push_back(data); } filein.close();Output when the file starts empty:
-89545645
23
45
34