What's a good coding style for reading different bits of data from a binary file in C? -


I'm a newbie programmer and writing a simple wav-player as a pet project in C. Part of the file loading process requires reading specific data (sampling rate, number of channels, ...) from the file title. Currently what I am doing is like this:

  1. Scan for the sequence of bytes and leave it last
  2. Read 2 bytes in the variable
  3. Skip 4 bytes
  4. Read 4 bytes in the variable
  5. Check the value and return to error < / Li>

    ... and so on. (See code :)

    I have written a number of helpful works to bite scanning / skipping / reading. Nevertheless, I am reading, checking, skipping the episode, which is neither too much Not only effective nor very smart. This is not a real issue for my project, but as it is a common task in handling binary files, I was wondering: Is there a pattern on how to do it more effectively with cleaner code?

    Often, people define structures (often #pragma pack (1) assurance against some padding) that matches the structure of the file, then they write something like fread , and use the value from the structure.

Comments