Hi everyone,
I'm currently working on a cross-platform project to copy data from the computer onto a USB drive quickly.
Main problem: EOF in the middle of the file
It's going good so far, but I stumbled across a problem in my copy_file function:
int copy_file(char *source_path, char *destination_path) {
FILE *input;
FILE *output;
char temp1[MAX_PATH] = {0};
strcpy(temp1, source_path);
char temp2[MAX_PATH] = {0};
strcpy(temp2, destination_path);
if ( (input = fopen(temp1, "r")) != NULL ) {
if ( (output = fopen(temp2, "w+")) != NULL ) {
/* Copying source to destination: */
char c;
while ( (c = getc(input)) != EOF ) {
putc(c, output);
}
} else {
/* If the output file can't be opened: */
fclose(input);
return 1;
}
} else {
/* If the input file can't be opened: */
return 1;
}
return 0;
}
It works fine for regular formatted files such as WORD-documents and text files, but with movies it stops after about 705 bytes, although the file is 42MB. I think it's because there is a EOF character in the middle of the file which breaks up the while loop. Does anyone know how to solve this?
Secondary issue: speed
In regards to speed, I need to write code that gets the job done as fast as possible. This was the simplest copy function I could think of, but I don't know whether it is the fastest. What is faster, fread/fwrite or getc/putc ? Or is there another standard function that can copy even faster?
I'm also not sure what the bottleneck is in regards to speed, as an portable drive takes more time to write to than for example internal memory. If I write a function that compresses files before they are copied, would that increase speed? Or would it just slow the read/write process down?
Thanks in advance,
~G