The two pieces of codes seemed as the same, but the output results are different.
This one goes correctly:
#include <stdio.h>
main()
{
FILE *fp = fopen("test.txt", "rb");
unsigned char buf[4];
while (fread(buf, 1, 1, fp))
{
printf("%s", buf);
memset(buf, 0, 1);
}
}
This one is incorrect:
#include <stdio.h>
void read(FILE *fp)
{
unsigned char buf[4];
while (fread(buf, 1, 1, fp))
{
printf("%s", buf);
memset(buf, 0, 1);
}
}
main()
{
FILE *fp = fopen("test.txt", "rb");
read(fp);
}
What's wrong with the second one?