Trouble with while(getline(file, str))

1
2
3
4
5
6
7
8
9
10
11
12
void parseFile(std::ifstream &file, std::string &string){
    std::ifstream file;
    file.open("text.txt");
    if(file.is_open()){
        std::string str = "";
        while(std::getline(file, str)){
            if(str == string){
                //do something, but the debugger never makes it here
            }  
        }
    }
}


I'm trying to iterate through each line of a file, but the code doesn't seem to be entering the while statement. I've placed a breakpoint on line 7 and the breakpoint is never even hit, yet the breakpoint on line 6 is hit, implying that the file is indeed open and working.

Edit: I know that it might seem likely that my file is simply empty or corrupt, so I'm just making it known that the file does indeed have content, and I have been successful in reading some elements from the file elsewhere in the program, but for some reason this loop is not working.
Last edited on
Well yes that definitely needed to be fixed lol. I was just typing into the window directly, not copying and pasting from my actual code. I fixed it in the example so as not to cause any more confusion.

So the original problem still stands.
I actually sort of answered it myself. Since I had already parsed the file once, I had to clear the ifstream's flags and then set the marker to the beginning of the file.

1
2
file.clear();
file.seekg(file.beg);
Topic archived. No new replies allowed.