문제

My program read a *.txt file and prints a line from the file backwards. i wanted to get the linenumber before it does so. after doing the loop, i got my number, but everything after my "for loop" is not printing/ working. what is wrong with my code?

    if (testFile.good()) {

        int countLines = 0;
        string temp;

        for (int i = 0; getline(testFile, temp); i++)
                    countLines++;

        cout << countLines;

        aline.readLine(testFile);
}
도움이 되었습니까?

해결책

Reset the stream flags so that you can "do stuffs" again in aline.readLine(testFile); after for loop

testFile.clear();
testFile.seekg(0, testFile.beg);

다른 팁

When you call getline(testFile, temp) repeatedly until it reaches the end of the file,

then you can not read more.

you need to rewind it to the beginning of the file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top