This project is in c++. I am trying to move along a textfile, and print each time a substr of 80 characters long on the screen. The next step would be to update the start position of x and then print again. My goal is to ‘slide’ over a txt. When I start the second stream operation I don’t get an error but there is no text outputted on the screen anymore.
Anyone who knows what i am doing wrong? Do i have to close the file before performing a new operation? Thanx
ifstream ifs("data.txt");
string line;
string subline;
int x=5;
while(getline(ifs,line)) {
subline=line.substr(x,80);
cout << subline;
}
system("pause");
system("cls");
x++;
//my issue!
while(getline(ifs,line)) {
subline=line.substr(x,80);
cout << subline;
}
You’re reaching the end of the file and it’ll be setting the ‘eof‘ state bit (and removing the ‘good‘ state bit). That’ll mean basically all subsequent read operations fail.
easy fix is to add after the x++: