-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
  • 36,642
  • 31
  • 139
  • 196

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
  • 6,610
  • 7
  • 40
  • 79