An interesting thing about fread().

C

Claude Yih

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.
 
A

Ashwani

Claude said:
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.
 
J

jdorrington

Claude said:
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.
 
E

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.
 
K

Kenneth Brody

Claude said:
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:[email protected]>
 
M

M. =?ISO-8859-1?Q?=C5hman?=

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:

#define FILENAME "test.txt"

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);
}

===================== 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
 
K

Keith Thompson

Claude Yih said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top