I am trying to copy a file from a specified library to the current directory. I can copy text files perfectly. Any other files become corrupt. The program detects a feof before it should.
#include <stdio.h>
int BUFFER_SIZE = 1024;
FILE *source;
FILE *destination;
int n;
int count = 0;
int written = 0;
int main() {
unsigned char buffer[BUFFER_SIZE];
source = fopen("./library/rfc1350.txt", "r");
if (source) {
destination = fopen("rfc1350.txt", "w");
while (!feof(source)) {
n = fread(buffer, 1, BUFFER_SIZE, source);
count += n;
printf("n = %d
", n);
fwrite(buffer, 1, n, destination);
}
printf("%d bytes read from library.
", count);
} else {
printf("fail
");
}
fclose(source);
fclose(destination);
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…