fread() and fwrite() are advised to me as a way to do fast IO. They are really useful. But I met two problems about them. My platform is Win64, Mingw32, gcc-3.4.5.
Problem I
-------------------------------------
c reference of fread() says:
If this number differs from the count parameter, either a reading error occurred or the end-of-file was reached while reading.
Seems fread() will stop if reaches EOF. And I wrote:
Code:
#include <stdio.h>
#define IOBUFSIZ 100000char inbuf[IOBUFSIZ];
int main(int argc, char const *argv[])
{
fread(inbuf, ~0u, 1, stdin);
// do something ....
return 0;
}
But it didn't read anything. The buffer is empty. And I wrote:
Code:
fread(inbuf, IOBUFSIZ, sizeof(char), stdin);
Success.
Why
Code:
fread(inbuf, ~0u, 1, stdin);
didn't work?
Problem II
------------------------------
I turned to study fwrite():
Code:
#include <stdio.h>
#define IOBUFSIZ = 100000;
char outbuf[IOBUFSIZ];
int main(int argc, char const *argv[])
{
// do something ...
fwrite(outbuf, sizeof(outbuf), 1, stdout);
return 0;
}
outbuf[] is global so it should be filled with zero. But the code print not only what I want to print, but also mountains of 'a' character following by. Where did the 'a' s came from?
Thanks you for spending your time reading my post.
Regards