Read data from files using fstreams

  • Thread starter Thread starter =?ISO-8859-1?Q?=22Jos=E9_Manuel_=28JMN_CandyBeat=2
  • Start date Start date
?

=?ISO-8859-1?Q?=22Jos=E9_Manuel_=28JMN_CandyBeat=2

Hello.

I'm moving from Visual C++ 6.0 to Visual C++ .NET 2003 and I saw the
changes to the streams. I'm rewriting my code and finished doing it
yesterday.

All seemed to be right, but the file reading.

My old code has lines like the following function call:

(file is a ifstream, var is a WORD, that is a typedef unsigned sort)
here is the line:


file.read(&var, 2)



Well, when this line is executed, the results are not what I'm looking
for: it reads inconsistent (arbitrary?) data. In my old code this line
worked. It is supposed to read 2 bytes from the file and asign them to
the WORD in var.

For this reason, I think that Visual C++ 6.0 and .NET 2003 has some
slight differences for reading in the way I want to read.

Maybe I'm not properly using the function. I mean... maybe I don't
understand well the mechanism in C++ to read BYTES, WORDS, DWORDS
UWORDS, etc.

So I request your help and would be thankful is someboy teach me about
reading using standar C++ streams in this .NET 2003.

Thank you very much.
 
José Manuel (JMN CandyBeat) said:
My old code has lines like the following function call:

(file is a ifstream, var is a WORD, that is a typedef unsigned sort)
here is the line:


file.read(&var, 2)



Well, when this line is executed, the results are not what I'm looking
for: it reads inconsistent (arbitrary?) data.

Are you opening the file in text mode (without ios::binary flag) by any
chance? What you see may be caused by CRLF to \n translation.
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
 
Igor said:
Are you opening the file in text mode (without ios::binary flag) by any
chance? What you see may be caused by CRLF to \n translation.

Whops! I solved the problem, it's because I was reading the wrong
position in the file and that explains why the reads was incoherent!

(By the way, I was using ios::binary | ios::in)

Thank you!!
 
Back
Top