Read from stdin write to stdout in C

后端 未结 4 716
无人及你
无人及你 2021-02-03 15:55

I am trying to write a cat clone to exercise C, I have this code:

#include 
#define BLOCK_SIZE 512
int main(int argc, const char *argv[])
{
    if         


        
4条回答
  •  醉话见心
    2021-02-03 16:21

    Your problem appears to be the return value of fread. I modified your code to print out the value bytes, and I get 0 every time. The man page for fread makes it clear that the return value of fread is NOT the number of characters. If EOF is encountered the return value could be zero (which it is in this case). This is because you are attempting to read in 1 thing that is size BLOCK_SIZE rather than BLOCK_SIZE things that are size 1.

提交回复
热议问题