Read ints from a binary file that has 2560 numbers in it

This is a discussion on Read ints from a binary file that has 2560 numbers in it within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { FILE *fp; int foo[10]; int i; if ((fp = fopen("xyz.set", "rb")) == NULL) { ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    16

    Read ints from a binary file that has 2560 numbers in it

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        FILE *fp;
        int foo[10];
        int i;
    
    
        if ((fp = fopen("xyz.set", "rb")) == NULL) {
        puts("Error: Input file invalid");
        return -1;
        } else {
        for (i = 0; i < 10; i+=5) {
            fread(&foo[i], sizeof(int), 5, fp);   
        }
        }
    
    
        fclose(fp);
    
    
        for (i = 0; i < 10; i++) {
        printf("%d ", foo[i]);
        }
        putchar('\n');
    
    
        return 0;
    }

    I need to read in a file that has binary ints of 4 bytes (2560 total ints) each and i'm stuck with the above code. Any help will be appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Are the ints in machine-readable or human-readable format? If they've been dumped by fwrite and appear as garbage characters in a text editor, your approach should work if you read one at a time:
    Code:
    for (i = 0; i < 10; i++)
        fread(&foo[i], sizeof(int), 1, fp);
    assuming that you wrote them the same way and endianness isn't an issue.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    yup they appear as garbage characters in a text editor

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    This is the output that I get.
    Attached Images Attached Images  

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,774
    Is the size of ints on your machine 4 bytes?

    Should you be using unsigned instead of signed ints?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    i'm not entirely sure of that, so u think i should use buffersize instead ?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,774
    Quote Originally Posted by jackofalltrades View Post
    i'm not entirely sure of that, so u think i should use buffersize instead ?
    I think you NEED to know something about the operating system and the compiler your are using!

    You also need to know what format the binary data is in.
    The format includes whether the data is signed or unsigned!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    I haven't tested your code myself, but it looks fine to me. You could even replace the for loop with a single call to fread (assuming foo is large enough for 2560 ints):
    Code:
    fread(foo, sizeof(int), 2560, fp);
    This will work only if the values in the file are in the same binary format (size and endianness) as the machine you're running on. You'll need to find out exactly what format the data is in before you can successfully read from the file.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    16
    I have got the code working, however i'm having issues with writing them to a file... does anyone have suggestions ?

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Yes, I suggest you let us know how you're writing to a file so we can offer constructive criticism.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 03:29 PM
  2. Read in numbers from file
    By ElNino in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 01:47 PM
  3. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2011, 07:46 AM
  4. Reading a binary file like a matrix of ints
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 06-26-2010, 10:45 AM
  5. Replies: 15
    Last Post: 12-17-2008, 12:11 AM

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