Copy files in C


/*
Write a program in C to copy files.
*/


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
char ch, file1[20], file2[20];
FILE *fs,*ft;

printf("Enter name of file to copy ");
gets(file1);

fs = fopen(file1,"r");
if( fs == NULL )
{
perror("Error ");
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}

printf("Enter name of target file ");
gets(file2);

ft = fopen(file2,"w");
if( ft == NULL )
{
perror("Error ");
fclose(fs);
printf("Press any key to exit...\n");
getch();
exit(EXIT_FAILURE);
}

while( ( ch = fgetc(fs) ) != EOF )
fputc(ch,ft);

printf("File copied successfully.\n");

getch();
fclose(fs);
fclose(ft);
return 0;
}

Post a Comment

0 Comments