Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > An interesting thing about fread().

Reply
Thread Tools

An interesting thing about fread().

 
 
Claude Yih
Guest
Posts: n/a
 
      04-13-2006
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.

 
Reply With Quote
 
 
 
 
Ashwani
Guest
Posts: n/a
 
      04-13-2006
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.

 
Reply With Quote
 
 
 
 
jdorrington@hotmail.com
Guest
Posts: n/a
 
      04-13-2006
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.

 
Reply With Quote
 
eml
Guest
Posts: n/a
 
      04-13-2006
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.

 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      04-13-2006
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: <(E-Mail Removed)>

 
Reply With Quote
 
M. =?ISO-8859-1?Q?=C5hman?=
Guest
Posts: n/a
 
      04-13-2006
On 13 Apr 2006 03:15:48 -0700
"Claude Yih" <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-13-2006
"Claude Yih" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting thing about the Panasonic GF1(Canada) RichA Digital Photography 0 05-15-2010 09:26 PM
one thing solved, but other terrible thing occur... Zam ASP General 1 03-14-2005 06:09 PM
Interesting thing at Best Buy.... Shinner DVD Video 151 12-25-2003 07:38 AM
Re: Q. What's the 'most interesting thing' that can be done with plain html ? spaghetti HTML 6 08-09-2003 07:58 PM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments