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>