[C] File reading and writing - image won't copy properly
Hello, I have written the following, as I would expect this to copy and paste the contents of one image file into another. Although it gets through all the bytes of the cat.png (517,213), the output image cat2.png does not contain as many bytes (516,096). If I keep a running total inside the loop, the counter is even greater than 517,213. If I limit the buffer size to 1, it equals 517,213 exactly - and still there is information missing from the image. It is odd too, the image displays, and is missing the feet on the cat.
Thanks for any insight onto what is happening.
//buffer to copy
char buffer[MAX_BUFFER_SIZE];
//input/output files
FILE* imgFile;
imgFile = fopen("cat.png","rb");
FILE* out;
out = fopen("cat2.png","wb");
//breaks when done reading imgFile
while(!feof(imgFile)) {
//read bytes from imgFile to buffer
fread(buffer, sizeof(char), sizeof(buffer), imgFile);
//write bytes from buffer to out
fwrite(buffer, sizeof(char), sizeof(buffer), out);
}
Posted Jan 27, 2016, 6:07 AM UTC