An interesting thing about fread().

Discussion in 'C Programming' started by Claude Yih, Apr 13, 2006.

  1. Claude Yih

    Claude Yih Guest

    Hi, everyone. I noticed an interesting thing about fread() this
    afternoon. Well, I can't see why so I post this message in the hope of
    getting some explanation. Please help me.

    I wrote the following code in Windows 2k and compiled it with the
    gcc(version: 3.2.3) contained in MinGW:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/stat.h>

    #define FILENAME "test.txt"
    #define BUFSIZE 1024

    void exit_error(const char*);

    int main(void)
    {
    FILE* fp = NULL;
    int count = 0;
    int rdbytes = 0;
    int filesize = 0;
    unsigned char buffer[BUFSIZE];
    unsigned char* chptr = NULL;
    struct stat st;

    if ((chptr = memset(buffer, '\0', BUFSIZE)) == NULL)
    {
    exit_error("memset:");
    }

    if ((fp = fopen(FILENAME, "r")) == NULL)
    {
    exit_error("fopen:");
    }

    if(stat(FILENAME,&st)==0)
    {
    filesize=st.st_size;
    printf("The size of this file is %d.\n", filesize);
    }

    while ((rdbytes = fread(buffer, sizeof(unsigned char), sizeof(buffer),
    fp)) > 0)
    {
    printf("%d bytes is got.\n",rdbytes);
    count += rdbytes;
    if ((chptr = memset(buffer, '\0', BUFSIZE)) == NULL)
    {
    exit_error("memset:");
    }
    }
    printf("%d bytes has been read from the file.\n", count);
    fclose(fp);
    exit(EXIT_SUCCESS);
    }

    void exit_error(const char* msg)
    {
    perror(msg);
    printf("\n");
    exit(EXIT_FAILURE);
    }

    ===================== end of the code =======================
    The result I got is displayed as follows:

    The size of this file is 4976.
    1024 bytes is got.
    1024 bytes is got.
    1024 bytes is got.
    1024 bytes is got.
    744 bytes is got.
    4840 bytes has been read from the file.

    ===================== end of the result =======================

    The interesting thing, I mean the question is
    why the sum of the bytes that fread() read does not equal with the size
    of the file?

    Every time I use fread(), I always assume fread() could be reliable.
    However, I can't trust fread() that much any more because of the above
    code :(

    Can anybody explain why that happened? Thanks very much.
    Claude Yih, Apr 13, 2006
    #1
    1. Advertising

  2. Claude Yih

    Ashwani Guest

    Claude Yih wrote:
    > Hi, everyone. I noticed an interesting thing about fread() this
    > afternoon. Well, I can't see why so I post this message in the hope of
    > getting some explanation. Please help me.
    >
    > I wrote the following code in Windows 2k and compiled it with the
    > gcc(version: 3.2.3) contained in MinGW:
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    > #include <sys/stat.h>
    >
    > #define FILENAME "test.txt"
    > #define BUFSIZE 1024
    >
    > void exit_error(const char*);
    >
    > int main(void)
    > {
    > FILE* fp = NULL;
    > int count = 0;
    > int rdbytes = 0;
    > int filesize = 0;
    > unsigned char buffer[BUFSIZE];
    > unsigned char* chptr = NULL;
    > struct stat st;
    >
    > if ((chptr = memset(buffer, '\0', BUFSIZE)) == NULL)
    > {
    > exit_error("memset:");
    > }
    >
    > if ((fp = fopen(FILENAME, "r")) == NULL)
    > {
    > exit_error("fopen:");
    > }
    >
    > if(stat(FILENAME,&st)==0)
    > {
    > filesize=st.st_size;
    > printf("The size of this file is %d.\n", filesize);
    > }
    >
    > while ((rdbytes = fread(buffer, sizeof(unsigned char), sizeof(buffer),
    > fp)) > 0)
    > {
    > printf("%d bytes is got.\n",rdbytes);
    > count += rdbytes;
    > if ((chptr = memset(buffer, '\0', BUFSIZE)) == NULL)
    > {
    > exit_error("memset:");
    > }
    > }
    > printf("%d bytes has been read from the file.\n", count);
    > fclose(fp);
    > exit(EXIT_SUCCESS);
    > }
    >
    > void exit_error(const char* msg)
    > {
    > perror(msg);
    > printf("\n");
    > exit(EXIT_FAILURE);
    > }
    >
    > ===================== end of the code =======================
    > The result I got is displayed as follows:
    >
    > The size of this file is 4976.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 744 bytes is got.
    > 4840 bytes has been read from the file.
    >
    > ===================== end of the result =======================
    >
    > The interesting thing, I mean the question is
    > why the sum of the bytes that fread() read does not equal with the size
    > of the file?
    >
    > Every time I use fread(), I always assume fread() could be reliable.
    > However, I can't trust fread() that much any more because of the above
    > code :(
    >
    > Can anybody explain why that happened? Thanks very much.



    I tried your program. To me it works fine and I get the the same no of
    bytes eitherways.
    Ashwani, Apr 13, 2006
    #2
    1. Advertising

  3. Claude Yih

    Guest

    Claude Yih wrote:
    >


    snip

    > printf("The size of this file is %d.\n", filesize);


    snip

    > printf("%d bytes has been read from the file.\n", count);


    snip

    > The result I got is displayed as follows:
    >
    > The size of this file is 4976.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 744 bytes is got.
    > 4840 bytes has been read from the file.


    Which of those two totals matches the actual filesize in a directory
    listing? Perhaps st.st_size is incorrect whilst fread(..) is returning
    the correct number of bytes.
    , Apr 13, 2006
    #3
  4. Claude Yih

    eml Guest

    I'm not sure about this, and please accept my apologies if I'm wrong.
    But perhaps it has something to do with the NTFS-filesystem
    and its fileheaders? I'm sorry again if im totally of track.
    eml, Apr 13, 2006
    #4
  5. Claude Yih wrote:
    >
    > Hi, everyone. I noticed an interesting thing about fread() this
    > afternoon. Well, I can't see why so I post this message in the hope of
    > getting some explanation. Please help me.
    >
    > I wrote the following code in Windows 2k and compiled it with the
    > gcc(version: 3.2.3) contained in MinGW:

    [...]
    > ===================== end of the code =======================
    > The result I got is displayed as follows:
    >
    > The size of this file is 4976.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 744 bytes is got.
    > 4840 bytes has been read from the file.
    >
    > ===================== end of the result =======================
    >
    > The interesting thing, I mean the question is
    > why the sum of the bytes that fread() read does not equal with the size
    > of the file?

    [...]

    You have opened the file in text mode. Under Windows, a text file has
    two characters for end-of-line (CR+LF -- "\r\n"), but the C library will
    strip the CR when reading a file open in text mode, so that the program
    will only see LF ('\n').

    The stat() call is returning the "real" size if the file, while the
    returns from fread() have stripped the CR.

    If you were to examine the file, you would probably see that it has
    136 (4976-4840) lines in it.

    As a test, change the mode passed to fopen() from "r" to "rb", to open
    the file in binary mode. Now, the CR's won't be stripped, and the
    lengths will be equal.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Apr 13, 2006
    #5
  6. On 13 Apr 2006 03:15:48 -0700
    "Claude Yih" <> wrote:

    > Hi, everyone. I noticed an interesting thing about fread() this
    > afternoon. Well, I can't see why so I post this message in the hope of
    > getting some explanation. Please help me.
    >
    > I wrote the following code in Windows 2k and compiled it with the
    > gcc(version: 3.2.3) contained in MinGW:


    <snip>

    > #define FILENAME "test.txt"


    <snip>

    > if ((fp = fopen(FILENAME, "r")) == NULL)
    > {
    > exit_error("fopen:");
    > }


    <snip>

    > if(stat(FILENAME,&st)==0)
    > {
    > filesize=st.st_size;
    > printf("The size of this file is %d.\n", filesize);
    > }


    <snip>

    > ===================== end of the code =======================
    > The result I got is displayed as follows:
    >
    > The size of this file is 4976.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 1024 bytes is got.
    > 744 bytes is got.
    > 4840 bytes has been read from the file.
    >
    > ===================== end of the result =======================
    >
    > The interesting thing, I mean the question is
    > why the sum of the bytes that fread() read does not equal with the size
    > of the file?
    >
    > Every time I use fread(), I always assume fread() could be reliable.
    > However, I can't trust fread() that much any more because of the above
    > code :(
    >
    > Can anybody explain why that happened? Thanks very much.
    >


    This is what you are looking for:

    http://c-faq.com/stdio/textvsbinary.html

    Magnus
    M. =?ISO-8859-1?Q?=C5hman?=, Apr 13, 2006
    #6
  7. "Claude Yih" <> writes:
    [...]
    > if ((chptr = memset(buffer, '\0', BUFSIZE)) == NULL)
    > {
    > exit_error("memset:");
    > }


    memset() doesn't return NULL to indicate an error; in fact, it has no
    mechanism for reporting errors. It just returns its first argument.

    --
    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, Apr 13, 2006
    #7
    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. news.amnet.net.au

    Equivalent of "fread" C function in Java

    news.amnet.net.au, Jan 15, 2004, in forum: Java
    Replies:
    2
    Views:
    4,743
  2. Patrick
    Replies:
    6
    Views:
    856
    Roedy Green
    Jul 13, 2004
  3. spaghetti
    Replies:
    6
    Views:
    365
    Jukka K. Korpela
    Aug 9, 2003
  4. Axel
    Replies:
    12
    Views:
    657
  5. Zam
    Replies:
    1
    Views:
    234
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page