Thread: Reading in numbers from a binary file

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Reading in numbers from a binary file

    Hello, I have a binary file and am trying to read in numbers. The problem is that for numbers that are two bytes, it ignores trailing 0s. For example, the number (in hex) 0x0200 is simply read in as integer of 2 instead of 512.

    Code:
    FILE *input;
    int test;
    
    input = fopen(argv[1], "rb");
    fread(&test, 2, 2, input);
    printf("Test 2 bytes: %d\n", test);
    Any advice would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Well, your fread call is saying "give me two 2-byte integers from the file handle input, and store them in a single integer, test". I presume you're on a 32-bit system so test is actually 4 bytes and can hold the data it read in. Perhaps you should create test as an array of 2 shorts to store these two short integer values and print out the value of test[0] or test[1].

    The other thing that I'm curious about is where you got the binary file. There is a chance that the file was from a big-endian machine, while yours is little endian: Endianness - Wikipedia, the free encyclopedia. A copy of the input file would be nice to test against if you have it.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for your help. So I tried that, and it seems to be outputting correctly.

    Code:
    short int test[2];
    
    fread(&test, 2, 2, input);
    printf("Test 1: %d\n", test[0]);
    printf("Test 2: %d\n", test[1]);
    Output:
    Code:
    Test 1: 2
    Test 2: 0
    The input file was created for the computer science course that I'm in, so I don't want to post it all. Here's a section of it though. The part I'm looking for starts at offset 8 and lasts for two bytes (and I know that it translates to 512).
    Code:
    offset    asc hex dec oct bin
    00000006:  F  46  070 106 01000110
    00000007:  S  53  083 123 01010011
    00000008:     02  002 002 00000010
    00000009:     00  000 000 00000000
    00000010:     00  000 000 00000000
    00000011:     00  000 000 00000000
    00000012:     19  025 031 00011001
    00000013:     00  000 000 00000000
    I have to do this kind of thing for the entire file, so I'm hoping that once I learn this part I'll be able to do the rest. By the way, I know for a fact that the file is in Big Endian form. I'm using Ubuntu for coding, so I guess that I'll have to convert (ntoh?) before I write back to the file.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If you want to start at offset eight (8) you should probably fseek to there before reading. Then read 2 bytes into a 2 element unsigned char array. Then do the math yourself such as array[0] * 256 + array[1]. That way you are not dependent on endianness of your current machine.
    Last edited by nonoob; 11-22-2010 at 03:23 PM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thank you for the advice. I actually am using fseek currently. I'm having a problem, though, trying to find the next value. I know that it takes up 4 bytes (from lines 10 to 13). When I use a char array to do this, however I get the values:
    Code:
    char[0] = 19
    char[1] = 0
    char[2] = 0
    char[3] = 0
    Clearly this is not even close to the hex file above for line 10-13 (final value is 6400). The code for this is just:
    Code:
    fseek(input, 10, SEEK_SET);
    unsigned char test[4];
    fread(&test, 4, 4, input);
    Last edited by Tagg; 11-22-2010 at 04:15 PM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Should be
    Code:
    fread(&test, 1, 4, input);
    That way you are reading 4 bytes instead of 16. Your buffer is only 4 bytes long.
    Maybe that's where your problem lies if you expect the NEXT fread to proceed immediately where you thought the previous one would leave off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary file reading.....
    By roaan in forum C Programming
    Replies: 13
    Last Post: 08-27-2009, 11:43 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Reading in a binary file
    By Bri Rock in forum C++ Programming
    Replies: 3
    Last Post: 07-08-2004, 03:54 PM