Binary file problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • afrin216
    New Member
    • Dec 2006
    • 2

    Binary file problems

    Hi,

    I'm a beginner in programming with C++. Well I've built a program which requires extensive usage of files and I've been using binary file formats and fread and fwrite commands to perfection.

    The project was going well till the curtain was dropped.ie of lately I've been getting junk output whenever I used fread. I noticed that I got the error only when the number of objects I wrote into was more than 1. There is no error when I read the first object. But there after I get a series of garbage value, and I'm finding it most irritable and tiresome as I cannot find out what the problem is. I've converted most of my program to use text files, but its getting much complicated using txt files.

    Please evaluate my problem and tell me how to get off this mess.

    Afrin
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Seeing the code producing the corrupt data would help.

    There is no reason why fread can't read multiple records.

    Comment

    • macklin01
      New Member
      • Aug 2005
      • 145

      #3
      Are you writing and reading/viewing on different architectures? If the size is greater than one, then endianness matters. Most stuff we'll run on is little-endian (e.g., x86, Intel-based Mac), but there's plenty of big-endian stuff out there (e.g., Motorola- and PPC-based Mac, Sun Sparc).

      Here's a quick endianness check:
      Code:
      bool IsBigEndian()
      {
       short word = 0x0001;
       if((*(char *)& word) != 0x01 )
       { return true; }
       return false;
      }
      Run it on the platform where you're writing your file. Does it return true? If so, it's big-endian.

      Run it again on the platform where you're reading/viewing your file. Does it return a different value? If so, endianness may be the issue.

      Please forgive me if I've made any mistakes here--I'm very tired!! :) -- Paul

      Comment

      Working...