Tech Support > Microsoft Windows > Development Resources > fstream doesn't read my file right.
fstream doesn't read my file right.
Posted by MerdCH 555 on August 27th, 2004


I don't understant this:

ifstream fin(file.dat,ios::in);
unsigned char temp;
int a;
fin >> temp; //or temp=fin.get();
a=int(temp);


This code reads the file perfectly and gives the ASCII number of a
character just fine, EXCEPT when the character in file is 10-13 by its
ASCII number, then a can be almost anything usually about 100.

Why?
How to correct this?

hmm..bizzare

compiler is the free Borland 5.5.1 for Win32

Posted by Mike Wahler on August 28th, 2004



"MerdCH 555" <Merodach85@yahoo.com> wrote in message
news:213e7.0408271238.4f55eb15@posting.google.com. ..
By '10-13', I assume you mean the end-of-line sequence 'CR/LF', i.e.
0x0D and 0x0A on a Windows platform. Note that these characters are
in the set comprising 'whitespace'. The istream:peator>>() extractor
skips and discards any whitespace it encounters. So the character you're
seeing that you call 'wrong' is most likely the character following
the 'CR/LF' sequence. I think 100 is the encoding for some lower-case
alpha character in ASCII.

If you use 'istream::get()' as indicated in your comment, extraction
of 'CR/LF' should yield a single character with the value '\n' (its
actual numeric value depends upon the implementation, that's why we
use the abstraction '\n'.)

However if you open your file in binary mode ('std::ios::binary'),
then you'll get a one-to-one match with the characters in your
file and those actually stored in 'temp'.

See above.

Not bizzare at all.

-Mike