By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

An interesting thing about fread().

P: n/a
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.

Apr 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

Apr 13 '06 #2

P: n/a
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 '06 #3

P: n/a
eml
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.

Apr 13 '06 #4

P: n/a
On 13 Apr 2006 03:15:48 -0700
"Claude Yih" <wi******@gmail.com> 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
Apr 13 '06 #5

P: n/a
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:Th*************@gmail.com>

Apr 13 '06 #6

P: n/a
"Claude Yih" <wi******@gmail.com> 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) ks***@mib.org <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.
Apr 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.