2

Ok so this is killing me at the moment cause its such a simple part of my program that just doesn't want to work. I'm reading data from a textfile to use in a GA.

The first getline() works perfectly, but the second one doesn't want to write any data into my string. When i cout the string it doesn't show anything.

Here is the code:

ifstream inFile;
inFile.open(fname.c_str());
char pop[20], mut[20];

inFile.getline(pop,20);
cout << pop;
inFile.getline(mut,20);
cout << mut; //this outputs nothing

Thanks for any help in advance. A sample form my file: there is no line between them mutation is the line straight after population

Population size: 30
Mutation: 20

Robᵩ
  • 143,876
  • 16
  • 205
  • 276
Hendrik Human
  • 133
  • 1
  • 13
  • Just a hint: factor the `ifstream` out into a function accepting an `istream`, and write a unit test for it with a `stringstream` containing "a\nb". – xtofl Nov 04 '12 at 07:06
  • 1
    By the way, could you enlighten us with a peek at the first two lines of your input file? – xtofl Nov 04 '12 at 07:14

4 Answers4

3

Your file's first line is 20 characters long (19+new line) but pop[20] can only contain 19 (because the last one is reserved for the null terminator '\0').

When istream::getline stops because it has extracted 20-1 characters, it doesn't discard the new line delimiter (because it was never read). So the next getline just reads the end of the first line, discarding the new line.

That's why you get nothing in the second string.

Alex
  • 7,252
  • 3
  • 29
  • 58
2

Your problem is that the length of your input line exceeds the length of the buffer which must hold it.

The solution is to not use character arrays. This is C++, use std::string!

std::ifstream inFile;
inFile.open(fname.c_str());

std::string pop;
std::getline(inFile, pop);
cout << pop << "\n";

std::string mut;
std::getline(inFile, mut);
cout << mut << "\n";
Robᵩ
  • 143,876
  • 16
  • 205
  • 276
1

I think you need to find out what the problem is. Add error checking code to your getline calls, refactor the (simple) code into a (simple) function, with a (simple) unittest. Possibly, your second line is longer than the assumed 20 characters (null-term included!).

For an idea of what I mean, take a look at this snippet.

xtofl
  • 38,207
  • 10
  • 95
  • 177
  • I have done many unit tests to test every simple thing I could think of. I use a char array now because getline(inFile, line); doesnt work where line is a string, it just outputs nothing – Hendrik Human Nov 04 '12 at 07:27
  • 1
    Well the snippet you show us, when put into a function, works nicely. I added your input to my test - so possibly one of your input lines is longer than you thinkk. – xtofl Nov 04 '12 at 07:34
  • yes thanks I should just have changed getline to take 21 characters, +1. It's a shame though I can't get getline(inFile, line); to work – Hendrik Human Nov 04 '12 at 07:42
  • @user1763722 What the problem with `getline(inFile, line);`? That is the best way to do it, you should be asking about that, instead of trying inferior methods. – john Nov 04 '12 at 07:44
  • it just doesn't put anything inside of 'line'. Any string i use with that version of getline is blank – Hendrik Human Nov 04 '12 at 08:10
0

try something like

while (getline(in,line,'\n')){
    //do something with line
}

or try something like

string text;
string temp; 
ifstream file;
  file.open ("test_text.txt");

  while (!file.eof())
  {
    getline (file, temp);
    text.append (temp); // Added this line
  }
NullPoiиteя
  • 53,430
  • 22
  • 120
  • 137
  • ok i tried that code but it doesn't put any string inside temp, is there something wrong with my compiler? because a plain getline(file, line); doesn't work at all – Hendrik Human Nov 04 '12 at 07:12
  • 1
    Please don't recommend using `.eof()` as a loop condition. It [almost always produces buggy code](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), as it has in this case. – Robᵩ Nov 04 '12 at 08:50