getline returns whole file contents

Hi

in the code below the value of int x is always 1.
This leads me to believe that getline(openfile, string) reads in the whole file instead of a line at a time.

If so is this the correct behaviour and how do I just read in a line at a time from a file ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main() {

 ifstream in("/etc/hosts");
 string s, line;
 int x=1;
 while(getline(in, line))
        cout << x << "\t" << line << endl;
        x += 1;
}


Many thanks
Nick .
Please add checking that file is open.
1
2
3
4
5
6
7
8
if (in.is_open())
{
do something
}
else
{
error
}


I think that you don't have permissions to read this file. Please try to run the program by following way:

sudo ./myprogram
But any way you are wrong, getline reads only one string.
Hi Denis

any user can open /etc/hosts so that is not the issue, however I have changed the file to open a file created by myself instead, so the code is now

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main() {

 ifstream in("testfile.txt");
 string s, line;
 int x=1;
 while(getline(in, line))
        //s += line + "\n";
        cout << x << "\t" << line << endl;
        x += 1;
}


And here is testfile.txt
1
2
3
4
5
6
$ cat testfile.txt
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd
eeeeeeee


Here is the output of the program when run

1
2
3
4
5
6
7
$ ./fillstring
1       aaaaaaaa
1       bbbbbbbb
1       cccccccc
1       dddddddd
1       eeeeeeee
1


By adding the file is open check my code becomes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main() {

 ifstream in("testfile.txt");
 string s, line;
 int x=1;
 if (in.is_open()) {
        while(getline(in, line))
                //s += line + "\n";
                cout << x << "\t" << line << endl;
                x += 1;
 } else {
        cout << "Unable to open 'testfile.txt'." << endl;
 }
}


However the output is still the same

1
2
3
4
5
6
7
$ ./fillstring
1       aaaaaaaa
1       bbbbbbbb
1       cccccccc
1       dddddddd
1       eeeeeeee
1


Any idea what's going on ?

Thanks
Nick .


Do you understand what do you do?
Your are trying to open file with name "testfile.txt"
Then you check that opening file is success. In your case it's so, you opened file successfully and after that you call getline in loop. Where is problem?

Or did you mean your problem with /etc/hosts?
And did you try to run program with sudo?
Sorry, I've found the problem :)
It should be help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main() {

 ifstream in("testfile.txt");
 string s, line;
 int x=1;
 if (in.is_open()) {
        while(getline(in, line)) {
                //s += line + "\n";
                cout << x << "\t" << line << endl;
                ++x;
               }
 } else {
        cout << "Unable to open 'testfile.txt'." << endl;
 }
}
Hi Denis

unfortunately your amendment (line 15) still gives the same output.

Regarding the problem, I'm learning c++ and I want to understand why getline reads in the whole file at once rather than reading one line at a time.

If it was reading one line at a time then each line in the output would start with the value of x, which would increment, however x is staying at 1 which leads me to believe that the while loop is only getting called once. Does that make sense ?

I know I can get the output desired by using vectors but I can't see why it's not working with getline.

Thanks again,
Nick .
Found the problem, missing the brackets on the while loop so only the first item in the loop was getting evaluated - Ha.

Correct code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main() {

 ifstream in("testfile.txt");
 string line;
 int x=1;
 if (in.is_open()) {
        while(getline(in, line)) {
                cout << x << "\t" << line << endl;
                ++x;
        }
 } else {
        cout << "Unable to open 'testfile.txt'." << endl;
 }
}

Denis

I didn't notice those changes in your code, you were indeed correct. Thanks for your help.

Nick .
Topic archived. No new replies allowed.