Search code examples
cfilefile-iofwritefread

fread() doesn't work but fwrite() works?


Why fread() doesn't work but fwrite() works?

If I get fwrite() into comments and fread() out of comments, the output file is of 0 bytes size... But if fwrite() is out of comments, the output file is of 64 bytes size..

What is the matter?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *input = fopen( "01.wav", "rb");

        if(input == NULL)
        {
            printf("Unable to open wave file (input)\n");
            exit(EXIT_FAILURE);
        }

    FILE *output = fopen( "01_out.wav", "wb");


    //fread(output, sizeof(char), 64, input);
    fwrite(input, sizeof(char), 64, output);

    fclose(input);
    fclose(output);

    return 0;
}

Solution

  • You should read from your input file, and write to your output file.

    char buf[64];
    fread(buf, 1, sizeof(buf), input);
    fwrite(buf, 1, sizeof(buf), output);
    

    Your code should check the return values of fread and fwrite for errors.