You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
976 B

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 4096
int copyFile(const char *sourceFile,const char *targetFile)
{
FILE *fpR = NULL, *fpW = NULL;
char buffer[BUFFER_SIZE];
int lenR, lenW;
if ((fpR = fopen(sourceFile, "r")) == NULL)
{
printf("The file '%s' can not be opened! \n", sourceFile);
return -1;
}
if ((fpW = fopen(targetFile, "w")) == NULL)
{
printf("The file '%s' can not be opened! \n", targetFile);
fclose(fpR);
return -1;
}
memset(buffer,0,BUFFER_SIZE);
while ((lenR = fread(buffer, 1, BUFFER_SIZE, fpR)) > 0)
{
if ((lenW = fwrite(buffer, 1, lenR, fpW)) != lenR)
{
printf("Write to file '%s' failed!\n", targetFile);
fclose(fpR);
fclose(fpW);
return -1;
}
memset(buffer, 0, BUFFER_SIZE);
}
fflush(fpW);
fclose(fpR);
fclose(fpW);
return 0;
}