Thread: Read Bytes From File Into Integer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What are you talking about exactly? If you write an int to the file, read an int to the file and use sizeof(int). If you write a short to the file then read a short to the file and use sizeof(short). If you have an int (and int is 4 bytes on your system) then writing 2 bytes of the integer isn't always going to work. Different machines with different endianness store the bytes in different orders. For example, if you have a 4-byte int that's set to 255, it might look in memory like 0x00FF0000 or 0x000000FF or probably even different combinations. Anyway, unless you really know what you're doing, write the entire variable to the file and read the entire variable back from the file. And also note that if you try to read the int in from the file it might not be the right number if the file was created on a different computer.
    If you understand what you're doing, you're not learning anything.

  2. #2
    Chad Johnson
    Join Date
    May 2004
    Posts
    154
    Well hey thanks, I really do appreciate all the help from everybody. I will definitely look into the different options that you all showed me.

    Right now I am going to just hard-code the byte lengths, then I will go back and do the wrapping that you suggested or something to that effect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  4. Replies: 3
    Last Post: 03-02-2008, 12:33 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM