Reading numbers from a file

To read numbers from a file into a variable, there are two basic alternatives:

  • in a text file each byte, or char in the file represents one digit. In this case, the function fscanf can be used with an appropriate format string.
  • in a binary file the numbers are represented either in big or little endian order just as they are in memory. In this case, we need to know which endianness is used both in the file and in memory. If they are the same, we can simply use the function fread to read binary data, otherwise, we have to make a conversion after the call to fread.

See the book for details about these functions!

(I will add example code here soon…)

Leave a Reply

Your email address will not be published. Required fields are marked *