#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
usingnamespace std;
int main () {
string line;
ifstream myfile ("mydata.txt");
vector<vector<string> > dataTable;
if (myfile.is_open())
{
while (! myfile.eof() )
{
stringstream ss(line);
string Tag;
ss >> Tag;
cout << Tag << endl;
// if (???) break;
// I am stuck here.
// we also don't know in advance how many lines
// does the data contain
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
I'm guessing you don't have enough memory to store the entire file at once if it has 10^7 lines.
In that case, I'd suggest the following algorithm:
create an empty deque
while ( not at eof ) {
read one line from file
put the line at the end of the deque
if( deque has k + 1 lines in it ) {
pop the first element off the deque
process it
}
}
when the algorithm terminates, the deque will hold the last k lines of the file, unprocessed.