Like Tree1Likes
  • 1 Post By memcpy

Error-checking a class read from a binary file

This is a discussion on Error-checking a class read from a binary file within the C Programming forums, part of the General Programming Boards category; I'm writing a certain class (the specifics don't really pertain to my question) to a file like so Code: fwrite((void*)&Map[MapID], ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Error-checking a class read from a binary file

    I'm writing a certain class (the specifics don't really pertain to my question) to a file like so

    Code:
    fwrite((void*)&Map[MapID], sizeof(cMap), 1, fp);
    And I read it later like so

    Code:
    fread((void*)&Map[MapID], sizeof(cMap), 1, fp);
    Although I doubt anything will ever go wrong, how would I go about checking to make sure that the right information is being read? That the binary I'm reading is being mapped to the right parts of the structure, etc.? This code works, but I'd like to be able to check for consistency in case someone messes with files or something.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    333
    The data in the structure means something. Let's say its an employee. If the name isn't nul terminated, you know the data has been corrupted. If the salary is some huge or tiny figure then either you've got w weird and wonderful remunations policy, or the data is corrupted. If the payroll id doesn't match a valid payroll id, the data has been corrupted.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Ah! That makes sense, thank you very much.

    As a kind of follow up - if I was going to try to send the same data over a network, how would the server/client know that it's receiving a cMap structure? Specifically, I'm using SDL_Net, and the function to receive a message is SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen). This function returns the number of bytes read, so would I just see if it's the same as sizeof(cMap), or is the size not constant?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Presuming there are no pointers in the struct, there are two issues:

    1) The size of types such as int, which can vary. If you want to make sure the struct is portable, you need to use fixed size types such as int32_t, that are guaranteed to be the same size everywhere.

    2) Compiler padding: it generally adds a few bytes to the struct if it is not an even multiple of (eg.) 4 or 8 bytes (and possibly with regard to individual fields). That padding can, I think, be in between fields, and another compiler on another computer could do it differently. You want to make sure there is no padding at all, this way the sizeof() is exactly the same as the total of all fields. There is no standard way to do that, but all compilers (I think) have a way of doing it. Eg, for gcc:

    Code:
    #pragma pack(1)
    struct whatever {
    I believe that is applied per field, but as long as you use 1 as the argument it won't matter.

    Structure-Packing Pragmas - Using the GNU Compiler Collection (GCC)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Well, if you're using GCC, why wouldn't you use:
    Code:
    struct whatever {
    
    } __attribute__((__packed__));
    It's virtually the same thing, the only difference being that you can choose which structs are/are not padded.
    GReaper likes this.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    1
    I think that you would might want be interested in consistency checking, there are many options for that, most standard (sorting by simplicity) include: checksum, crc32, md5 and sha1.

    Just to give you the idea, try xoring each and every byte you of your array, in the end you'll have one byte which describes all the others, so you append that byte to the end of your buffer when saving, and after loading that back again you'll also read the checksum, so you can compute it back again against the array, if someone tampers with the data the checksum will not match.

    Google or check wikipedia about to get more elaborated answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read binary file
    By larne in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2009, 05:21 AM
  2. Binary read from file
    By dereach in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 03:34 PM
  3. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 12:12 PM
  4. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 03:36 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21