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