How to write a c program to copy a file from source file to destination file in C programming ?
Solution:
#include <stdio.h>
#include <stdlib.h>
void fileCopy(char *source, char *destination);
int main()
{
char sourceFile[] = "input.txt";
char destinationFile[] = "output.txt";
fileCopy(sourceFile, destinationFile);
return 0;
}
void fileCopy(char *source, char *destination)
{
FILE *fp1, *fp2;
int ch;
fp1 = fopen(source, "r");
fp2 = fopen(destination, "w");
if(fp1 == NULL || fp2 == NULL)
{
printf("Error opening file.\n");
exit(1);
}
while(1)
{
ch = fgetc(fp1);
if(ch == EOF) break;
else fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
remove(source);
}