This copies a file from one location to another, preserving the creation and last access times.
Code
//#############################################################################
void copy_file(char* source, char* destination)
{
FILE* infile;
FILE* outfile;
char* buffer[8192];
size_t count_read;
size_t count_written;
if(infile = fopen(source, "rb")) {
if (outfile = fopen(destination, "wb")) {
while (!feof(infile) && !ferror(outfile)) {
count_read = fread(buffer, 1, 8192, infile);
count_written = fwrite(buffer, 1, count_read, outfile);
}
fclose(outfile);
struct stat buf;
stat(source, &buf);
utimbuf times;
times.actime = buf.st_atime;
times.modtime = buf.st_mtime;
utime(destination, ×);
}
else {
printf("failed to open %s for output\n", destination);
}
fclose(infile);
}
else {
printf("failed to open %s for input\n", source);
}
}