[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: File input problem
I think I am a little late in replying to this but "better late than never
Use the 'while' loop as follows :- while (inFile.peek()!=EOF)
Everything else remains the same.
That extra character was coming because of the end of file character.Since
you are checking by the good() so it also copies the EOF into the array.But
the peek() checks the next character without actually extracting it.
Hope it solves your problem.
----- Original Message -----
From: Ashwin <ashwin_n@xxxxxxxxxxx>
To: LI Programmers <linux-india-programmers@xxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, November 19, 2000 12:48 PM
Subject: [LIP] File input problem
> 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;
> 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>
> <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
> line has this extra character.
> How do I correct this?
> The mailing list archives are available at
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com