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

Re: File input problem



try replacing the "while" loop with a "do-while" construct. of course
this asumes that ur file is not empty.

Ashwin wrote:
> 
> 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.
> <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.