[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

File input problem



Hi,

I'm having a very trivial problem while reading a file.

I'm using C++ and hence classes to read a file. Even though I'm 
checking for eof, I'm getting an extra character in the file's last line.

Here are the files I'm working with -

<C++ source code>
#include <fstream>

int main()
{
  ifstream inFile;
  char s[80];
  int i = 0;

  inFile.open( "test.txt", ios::in );

  while( inFile.good() )
  {
    s[i++] = inFile.get();
  }

  for( int j = 0; j < i; ++j )
    cout << s[j];
  cout << endl;

  return 0;
}
</C++ source code>

<test.txt contents>
linux
<test.txt contains *only* five characters>

But, the output I'm getting is

linux# <-- some extra character

Even in a multiline file all the lines are read correctly, but only the last 
line has this extra character.

How do I correct this?

Ashwin
ashwin_n@xxxxxxxxxxx