Problem with getline()

I have a problem with this C++ code:

1
2
3
4
5
6
7
8
ifile has been opened successfully.
...
std::string str;
...
ifile.ignore() ; // This appears to be necessary
std::getline(ifile,str,'\n') ;
while(std::getline(ifile,str,'\n'))
     std::cout << str << std::endl;


After the first call to getline, the value of str is “” although ifile is not empty and contains several lines.

The second call to getline causes the while loop to fail immediately.
Last edited on
Without parameters, line 5 ignores a single character. Is that what you want?
http://www.cplusplus.com/reference/istream/istream/ignore/

Are you sure that the first call to getline() succeeded?
Try printing the value of ifile.fail(). I think it's likely that something happened earlier in your code that invalidated the ifile stream.
Doing what I would expect to do.
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
$ cat foo.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  ifstream in("foo.cpp");
  string line;
  while ( getline(in,line) ) {
    cout << "L=" << line << endl;
  }
  return 0;
}
$ g++ foo.cpp
$ ./a.out 
L=#include <iostream>
L=#include <fstream>
L=#include <string>
L=using namespace std;
L=
L=int main()
L={
L=  ifstream in("foo.cpp");
L=  string line;
L=  while ( getline(in,line) ) {
L=    cout << "L=" << line << endl;
L=  }
L=  return 0;
L=}


No weird .ignore() or pre-fetch getline required.

So reduce your problem to an actual repeatable example, and save the mystery of your ... omitted code and the invisible content of your text file.
Hello GerryWolff,

I have revised your code a bit. The comments should help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//ifile has been opened successfully.  Are you sure?

//...  What actually happens here?

std::string str;

//... // What actually happens here?

inFile.ignore(); // This appears to be necessary. Only if there is a preceeding formatted input (inFile >> aVariable;).
// <--- Also the more preferred use is:
//inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

std::getline(inFile, str, '\n');

std::cout << "\n " << str << '\n' /*<< std::endl*/; // <--- Used for testing. Keeping is not needed. Can be commented out later snd kept for future use.

while (std::getline(inFile, str, '\n'))// <--- The "\n" is the default. You do not actually need it here.
	std::cout << "\n " << str << std::endl;

When I added the missing code I ended up with this:

 his is a test line 1.

 This is a test line 2.




 Press anykey to continue:


As you see the ignore statement is ignoring the first character of the input file and the first "grtline" is picking up what is left until it reaches the '\n". Not what you want. When I comment out the ignore statement I get this:

 This is a test line 1.

 This is a test line 2.




 Press anykey to continue:



As the comment says line 11 is the more preferred way of writing the ignore statement. And it does not matter if you use "std::cin" or "inFile" it works the same. And this is a more portable use that more people can use.

As salem c pointed out the "..." may shorten your code, butt it leaves out very useful information like:
How do you know if the input file is open?
And is there any formatted input that would cause the need for the ignore statement.

As dhayden pointed out your input stream may not have opened the file correctly or something may have caused it to become useless before you reach the code that you did post.

In the end it is much better to post code that can be compiled and tested as opposed to what you think is the problem.

Being new you have yet to learn that a problem may start somewhere before where you think the problem is. The same holds true for error messages. The problem is not always on the line of the error message, but could be on the line above. I once had a program where the error started ten lines above whare the compiler was telling me the error occurred.

Lastly if there is an input file for the program it helps to post it, or at least a fair sample, of the file.

Hope that helps,

Andy
Topic archived. No new replies allowed.