Reading from a file with getline, won't work the 2nd time?

So I added identical code to my project and the 2nd for loop won't print out "testing". userandPwNumb value is the same for both for loops. I tried cin.clear() and cin.sync() to see if that would some how help but it did not.

c = '0' + userAndPwNumb;


for (std::string line; std::getline(DataIn, line);){
if (sizeof(line)){
if (line[0] == c){
++numberOfUserBalancesCounter;

cout << "line[0] of line = " << line[0];
}
}
}


for (std::string line; std::getline(DataIn, line);){
if (sizeof(line)){
if (line[0] == c){
++numberOfUserBalancesCounter;

cout << "testing";
}

}

}
You need to post your entire code. We cannot understand what all the variables you are using if you don't post all of it. And please use code tags:
https://www.cplusplus.com/articles/jEywvCM9/#:~:text=To%20use%20code%20tags%20select,%5D%5B%2Fcode%5D%20around%20your%20code.
Hello CodePanther,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


As the TheToastersaid you need to post enough ,or all, of the program that can be compiled and tested along with the input file, or a a good sample if large, so that everyone can use the same information.

Based on what you did post I would say the the first for loop is reading the file until the "eof" bit is set. So in the second for loop the file stream is unusable.

Try adding
1
2
DataIn.clear();   // <--- Resets the state bits on the file stream.
DataIn.seekg(0);  // <--- Positions the file pointer to the beginning of the file. 

Insert between the for loops.

Andy
This works, the first seek I have inserted isn't actually required because the file is all set to go but your testing and series of file reads lends itself to a function and there's no harm in going to the start even if it's already there.

See https://en.cppreference.com/w/cpp/io/basic_istream/seekg

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>

int main()
{
    // OPEN THE FILE
    std::ifstream DataIn ("data_in.txt");
    if (DataIn.is_open())
    {
        std::cout << "Enter password: (Use 1234) : ";
        int userAndPwNumb; // 1234
        std::cin >> userAndPwNumb;
        std::string c = std::to_string(0) + std::to_string(userAndPwNumb); //01234
        
        std::string password;
        std::getline(DataIn, password);
        
        if(password == c)
        {
            std::cout << "Ready to go!\n";
        }
        else
        {
            std::cout << "Password failed.\n";
            DataIn.close();
            
            return -95;
        }
    }
    else
    {
        std::cout << "Unable to open file";
        return -99;
    }
    
    const int LIMIT{5};
    std::string line[LIMIT];
    
    int no_balances{0};
    DataIn.seekg(std::ios::beg);
    while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
    {
        std::cout
        << "line[" << no_balances << "] = "
        << line[no_balances] <<'\n';
        
        ++no_balances;
    }
    
    // AGAIN
    DataIn.seekg(std::ios::beg);
    no_balances = 0;
    while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
    {
        std::cout << "testing AGAIN\n";
        ++no_balances;
    }
    
    // AND AGAIN
    DataIn.seekg(std::ios::beg);
    no_balances = 0;
    while (no_balances < LIMIT && std::getline(DataIn, line[no_balances]))
    {
        std::cout << line[no_balances]
        << " *** TESTING and again ***\n";
        ++no_balances;
    }
    
    DataIn.close();
    
    return 0;
}

Enter password: (Use 1234) : 1234
Ready to go!
line[0] = 01234
line[1] = Dear Russians,  very
line[2] = history. The year  200
line[3] = all measured this date
line[4] = then after we grew up
testing AGAIN
testing AGAIN
testing AGAIN
testing AGAIN
testing AGAIN
01234 *** TESTING and again ***
Dear Russians,  very *** TESTING and again ***
history. The year  200 *** TESTING and again ***
all measured this date *** TESTING and again ***
then after we grew up *** TESTING and again ***
Program ended with exit code: 0


And the data_in.txt file - I suspect your data file isn't the same.

01234
Dear Russians,  very
history. The year  200
all measured this date
then after we grew up
mothers would  be, and
the extraordinary New
today  I am wishing
all. Today I am addres
made a decision. I hav
of the outgoing centur
Yeltsin will try to ho
anyone. That is all li


Last edited on
Topic archived. No new replies allowed.