-2

I am trying to read strings from a file with contents like:

r FFFF

r FF

r FFFF

Here is my code:

int main ()
{   int a; ifstream ifile; 
   ifile.open("hi.txt"); 
  while (!ifile.eof()) 
   { 
      ifile.getline(data, 100); // read a line from file
      a = strlen(data);
      cout<<'length"<<a;
    }
}

I am getting unexpected outputs: length7length5length6.

What am I doing wrong?

Andrei
  • 42,814
  • 35
  • 154
  • 218

1 Answers1

0

The '\n' (new line character) is in data. On windows you might also have a '\n\r' instead (or '\r\n' I don't remember)

on the last line you might not have it in your text file

Thomas
  • 8,306
  • 8
  • 53
  • 92