Their common syntax is:
[bbone=cpp,94]
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
size_t fread ( const void * ptr, size_t size, size_t count, FILE * stream );
[/bbone]
I understand fread reads total size * count bytes from the stream. Now, the problem is, does it matter if I switch the parameters size and count? The strange thing is, when I use something like this:
[bbone=cpp,95]#include
const int BUFF = 1000000;
char buff[BUFF];
int main() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int n = (int)fread(buff, 1, BUFF, stdin);
fwrite(buff, 1, n, stdout);
return 0;
}
[/bbone]
This works just fine, but if I switch 1 and BUFF, that is, fread(buff, BUFF, 1, stdin), it always returns 0, but it reads the stream successfully and sometimes it has repeating part in the end, not always. That's why I am quite confused about the behavior of these functions.
They provide a fast way to read/write IO files. Now, do their performance depend on how I pass the size and count?, to read 10000 bytes of data, I think this is better fread(ptr, 10000, 1, fp); but if I call like this, it causes problems.
Thanks in advance.
created
last reply
- 4
replies
- 832
views
- 3
users