Reading from stdin write to stdout in C

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

#include <stdio.h>
#define BLOCK_SIZE 512
int main(int argc, const char *argv[])
{
    if (argc == 1) { // copy stdin to stdout
        char buffer[BLOCK_SIZE];
        while(!feof(stdin)) {
            size_t bytes = fread(buffer, BLOCK_SIZE, sizeof(char),stdin);
            fwrite(buffer, bytes, sizeof(char),stdout);
        }
    }
    else printf("Not implemented.\n");
    return 0;
}

I tried echo "1..2..3.." | ./catand ./cat < garbage.txt, but I do not see the output on the terminal. What am I doing wrong here?

Edit: According to the comments and answers, I ended up with this:

void copy_stdin2stdout()
{
    char buffer[BLOCK_SIZE];
    for(;;) {
        size_t bytes = fread(buffer,  sizeof(char),BLOCK_SIZE,stdin);
        fwrite(buffer, sizeof(char), bytes, stdout);
        fflush(stdout);
        if (bytes < BLOCK_SIZE)
            if (feof(stdin))
                break;
    }

}
+5
source share
4 answers

I can quote a response from me: fooobar.com/questions/32433 / ...

fread(buffer, sizeof(char), block_size, stdin);
+7
source

fread. , , 0. man for fread , fread . EOF, ( ). - , 1 BLOCK_SIZE, BLOCK_SIZE, 1.

+2

fflush(stdout) fwrite()

+1

fflush; .

fread. BLOCK_SIZE 1 (sizeof (char) 1); , , 1 BLOCK_SIZE, , BLOCK_SIZE, fread 0. IOW, fread

size_t bytes = fread(buffer, 1, sizeof buffer, stdin);

fwrite.

+1

All Articles