Click to See Complete Forum and Search --> : write and read from a binary file


Maestro
March 11th, 2003, 07:24 AM
Hi!

A write out data to a binary file and later I want to read from it.
The problem is, when I use fread, it finds EOF somewhere in the middle of the file. I want to read int32 values when it occurs.

I think the hexa code of EOF is 04 and I had int32 i=4 data. Later I added 100 to every int at write and I subtracted 100, when I readed it, but the same thing happend.

How should I avoid that? Overstep it with fseek or what?

Thanx

Bob Davis
March 11th, 2003, 08:45 AM
Where are you getting this hex code 04 from? If you're using fread, you can just watch the returned value, which indicates how many bytes were read. If it is less than 4, you're probably at EOF, or a file error occurred. You can use feof() to find out which for sure.

Is there any reason why you couldn't use iostreams to accomplish the same thing? Reading integers from a binary file is a snap using ifstream, and the class has all of the functions that you're probably used to in your "C" standard library. I understand that whatever system you're coding for might not support C++, but if possible I would consider it.

Maestro
March 11th, 2003, 08:50 AM
fread() returns 0 and feof() returns non zero. I have to use standard C.

fellowDeveloper
March 14th, 2003, 07:04 PM
What type of access are you using when you open the file?

file = fopen("filename", "rb") should do the trick.

Maestro
March 19th, 2003, 07:31 AM
Ohh, that's it!

Thanx:D