Reading binary file with C

Discussion in 'C Programming' started by acp26b, Aug 2, 2006.

  1. acp26b

    acp26b Guest

    I need to read a binary to put into a struct. For some reason the code
    will not work. In this section of code i am just looping through the
    file to get the number of records so i can then build the correct size
    array. Nothing was happening so i used the variable test and found out
    that read is returning -1. Any ideas?

    char *inFileName = NULL;
    void *input;
    int fdi;
    int sizeOfData = sizeof(bin_record);

    fdi = open(inFileName, O_RDONLY);
    if (fdi == -1){
    printf("ERROR: UNABLE TO OPEN DATA FILE\n");
    exit (-1);
    }

    test = read(fdi, input, sizeOfData);
    //while( read(fdi, ptr, sizeOfData) > 0){
    // recordCount++;
    //}

    printf("%d \n", test);
    close (inFileName);
     
    acp26b, Aug 2, 2006
    #1
    1. Advertising

  2. acp26b

    Bill Pursell Guest

    acp26b wrote:
    > I need to read a binary to put into a struct. For some reason the code
    > will not work. In this section of code i am just looping through the
    > file to get the number of records so i can then build the correct size
    > array. Nothing was happening so i used the variable test and found out
    > that read is returning -1. Any ideas?
    >
    > char *inFileName = NULL;
    > void *input;
    > int fdi;
    > int sizeOfData = sizeof(bin_record);
    >
    > fdi = open(inFileName, O_RDONLY);
    > if (fdi == -1){
    > printf("ERROR: UNABLE TO OPEN DATA FILE\n");
    > exit (-1);
    > }
    >
    > test = read(fdi, input, sizeOfData);
    > //while( read(fdi, ptr, sizeOfData) > 0){
    > // recordCount++;
    > //}
    >
    > printf("%d \n", test);
    > close (inFileName);


    Replace the printf("%d\n", test); with perror(NULL). It may
    tell you the reason. For that matter, you might want to
    replace printf("ERROR: UNABLE TO OPEN DATA FILE\n");
    with perror(inFileName); It will give you a far more useful
    diagnostic.
     
    Bill Pursell, Aug 2, 2006
    #2
    1. Advertising

  3. "acp26b" <> writes:
    > I need to read a binary to put into a struct. For some reason the code
    > will not work. In this section of code i am just looping through the
    > file to get the number of records so i can then build the correct size
    > array. Nothing was happening so i used the variable test and found out
    > that read is returning -1. Any ideas?
    >
    > char *inFileName = NULL;
    > void *input;
    > int fdi;
    > int sizeOfData = sizeof(bin_record);
    >
    > fdi = open(inFileName, O_RDONLY);
    > if (fdi == -1){
    > printf("ERROR: UNABLE TO OPEN DATA FILE\n");
    > exit (-1);
    > }
    >
    > test = read(fdi, input, sizeOfData);
    > //while( read(fdi, ptr, sizeOfData) > 0){
    > // recordCount++;
    > //}
    >
    > printf("%d \n", test);
    > close (inFileName);


    open, read, and close are not standard C functions. The corresponding
    standard functions are fopen, fread, and fclose. If you can modify
    your program to use the standard functions, we'll be glad to help;
    otherise, try (probably) comp.unix.programmer.

    I note that the first argument to open() is a null pointer; you
    initialize inFileName to NULL, and you don't modify it -- at least in
    the code you've shown us.

    If you do set inFileName to some non-null value in code you haven't
    shown us, then you're asking us to guess what's wrong with your actual
    code based on some *other* code that you've posted. We're not
    psychic. Post a complete compilable program.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 2, 2006
    #3
  4. acp26b

    acp26b Guest

    i figured out the problem i was missing the & sign infront of the void
    pointer.

    About posting compilable code, the reason i did not do this was because
    to post full compilable code i would have had to post multiple files, i
    dont use fourms much and thought it would be better to only post the
    few lines that were causing the problem i thought it was understood
    that since it was opening the file and the problem was with the read
    that the file name was there. Sorry about that I will know better next
    time...


    Keith Thompson wrote:
    > "acp26b" <> writes:
    > > I need to read a binary to put into a struct. For some reason the code
    > > will not work. In this section of code i am just looping through the
    > > file to get the number of records so i can then build the correct size
    > > array. Nothing was happening so i used the variable test and found out
    > > that read is returning -1. Any ideas?
    > >
    > > char *inFileName = NULL;
    > > void *input;
    > > int fdi;
    > > int sizeOfData = sizeof(bin_record);
    > >
    > > fdi = open(inFileName, O_RDONLY);
    > > if (fdi == -1){
    > > printf("ERROR: UNABLE TO OPEN DATA FILE\n");
    > > exit (-1);
    > > }
    > >
    > > test = read(fdi, input, sizeOfData);
    > > //while( read(fdi, ptr, sizeOfData) > 0){
    > > // recordCount++;
    > > //}
    > >
    > > printf("%d \n", test);
    > > close (inFileName);

    >
    > open, read, and close are not standard C functions. The corresponding
    > standard functions are fopen, fread, and fclose. If you can modify
    > your program to use the standard functions, we'll be glad to help;
    > otherise, try (probably) comp.unix.programmer.
    >
    > I note that the first argument to open() is a null pointer; you
    > initialize inFileName to NULL, and you don't modify it -- at least in
    > the code you've shown us.
    >
    > If you do set inFileName to some non-null value in code you haven't
    > shown us, then you're asking us to guess what's wrong with your actual
    > code based on some *other* code that you've posted. We're not
    > psychic. Post a complete compilable program.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.
     
    acp26b, Aug 2, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    0
    Views:
    470
  2. El Durango

    reading binary file.

    El Durango, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    7,559
    Gordon Beaton
    Dec 8, 2004
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    934
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,689
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    735
Loading...

Share This Page