CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Posts
    19

    Unhappy write and read from a binary file

    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

  2. #2
    Join Date
    Jan 2001
    Posts
    588
    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    19
    fread() returns 0 and feof() returns non zero. I have to use standard C.

  4. #4
    Join Date
    Mar 2003
    Posts
    111
    What type of access are you using when you open the file?

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

  5. #5
    Join Date
    Apr 1999
    Posts
    19
    Ohh, that's it!

    Thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured