fread() function and reading binary files

Discussion in 'C Programming' started by Fernando Barsoba, Oct 17, 2005.

  1. Hi all,

    I'm trying to read from a binary file, and I'm using 'fread()' as
    indicated in function 'getfile()'. The variable 'bytesread' shows that I
    have read the 557,000 bytes from the file, but because the file has a 00
    at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17
    bytes. So, it's weird, because fread() seems to put 550,000 bytes in the
    buf variable, but after returning from the function, only bytes up to
    EOF are read..

    Any idea?

    Thanks,

    FBM

    main:
    ....
    void *buf2;

    buf2 = getfile(buf_trcd); // getfile() is defined below
    int lenbuf2;
    char buffer_length[20];
    lenbuf2 = strlen(buf2);
    sprintf(buffer_length, "%d", lenbuf2);
    len = send(p, buffer_length, strlen(buffer_length), 0);

    len = send(p, buf2, lenbuf2, 0);


    void * getfile(char *pathname) {
    FILE *fp;
    void *buf;
    long lSize;
    int bytesread;

    fp = open_file(pathname);
    // obtain file size.
    fseek (fp , 0 , SEEK_END);
    lSize = ftell (fp);
    rewind (fp);
    // allocate memory to contain the whole file.
    buf = malloc (lSize); // to do: free 'buf'
    if (buf == NULL)
    exit (1);

    // copy the file into the buffer.
    bytesread = fread (buf,1,lSize,fp);
    printf(buf);
    fclose(fp);
    return buf;
    }
     
    Fernando Barsoba, Oct 17, 2005
    #1
    1. Advertising

  2. Fernando Barsoba

    Malcolm Guest

    "Fernando Barsoba" <> wrote
    > I'm trying to read from a binary file, and I'm using 'fread()' as
    > indicated in function 'getfile()'. The variable 'bytesread' shows that I
    > have read the 557,000 bytes from the file, but because the file has a 00
    > at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17
    > bytes. So, it's weird, because fread() seems to put 550,000 bytes in the
    > buf variable, but after returning from the function, only bytes up to EOF
    > are read..
    >

    C strings are null-terminated. You probably have your data, after the first
    null.

    What you wnat to do is print it out in a loop including putc(), one
    character at a time.
    Any non-printing characters will show up in strange ways - you have to
    decide how to handle these in the final version.
     
    Malcolm, Oct 17, 2005
    #2
    1. Advertising

  3. Fernando Barsoba wrote:
    > Hi all,
    >
    > I'm trying to read from a binary file, and I'm using 'fread()' as
    > indicated in function 'getfile()'. The variable 'bytesread' shows that I
    > have read the 557,000 bytes from the file, but because the file has a 00
    > at the 17h position, when doing >>lenbuf2 = strlen(buf2), I get only 17
    > bytes.


    strlen() returns the length of a string. A string is terminated by the
    first 0 byte in it. It does not tell you the number of characters in
    your buffer. If you want to fill a buffer of unknown length in the
    function, have the number of characters as the return value or have a
    pointer to the number of characters as a parameter do one of those two
    with instead of simple number of characters variable a structure with a
    pointer to the buffer and its size as members.

    BTW, if your first 0 is at the 17h (0x17, 23, 027) position, you should
    not be getting 17 as the value from strlen(). I assume that was a typo
    for 17th.
     
    Martin Ambuhl, Oct 17, 2005
    #3
    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. Luc Holland

    Binary files, structs and fread

    Luc Holland, Jul 20, 2003, in forum: C Programming
    Replies:
    2
    Views:
    572
    Michael B Allen
    Jul 21, 2003
  2. syntax

    reading a file using fread()

    syntax, Feb 23, 2004, in forum: C Programming
    Replies:
    3
    Views:
    8,461
    Richard Bos
    Feb 24, 2004
  3. bill
    Replies:
    4
    Views:
    438
    Barry Schwarz
    Nov 9, 2005
  4. Replies:
    3
    Views:
    426
    CBFalconer
    Apr 13, 2007
  5. red floyd

    Re: fread not reading all information

    red floyd, Apr 12, 2007, in forum: C Programming
    Replies:
    4
    Views:
    346
    red floyd
    Apr 13, 2007
Loading...

Share This Page