Reading Lines from a txt file into a string

Say I have a commant.txt file that contains something like:
num_chars logfile.txt
num_lines settings.txt

I haven't included all my code because I think it's not necesary for what I need help with. But if you guys need it in my code understand it better I'll be sure to add it. Anyway...
Below is the code I used to get the first line which is "num_chars logfile.txt"
Then I read it into the string "result". After using that string certain functions I reset it back to an empty string so I can put the next line "num_lines settings.txt" into "result" and use it again. My problem is I can't seem to get the second line. I'm able to get the first line with the while loop below, but I'm not sure how to get the second line. I think it might be because i'm not using fin.get() properly but I'm not sure. My initial thought is that continuing to use fin.get() will continue reading the second line of code, but apparently it does not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int main()
{

    ifstream fin("commands.txt");


    string result = "";
    char c = fin.get();

    while (c != '\n')
    {
        result += c;
        c = fin.get();
    }
    cout << result << "\n"; 
    result = "";

    while (c != '\n')
    {
        result += c;
        c = fin.get();
    }
    cout << result << "\n"; 
    result = "";
 
Is there a reason you're using character extraction instead of extracting the entire line?

But the reason the second loop is not executing is because c is equal to '\n' from the prior loop.
Crap. I haven't thought about using getline(). This makes it much easier now. Thanks
Topic archived. No new replies allowed.