C Program To Copy Data From One File To Another
♣ Try it Out ::
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 25
main()
{
char ch, source[MAX], target[MAX];
FILE *fs, *ft;
printf("Type name of file to copy: ");
gets(source);
fs = fopen(source, "r");
if(fs == NULL)
{
perror("Error ");
getch();
exit(EXIT_FAILURE);
}
printf("Type name of target file: ");
gets(target);
ft = fopen(target, "r");
if(ft != NULL)
{
printf("The file exists. Overwrite it? (y/n) \n");
char con = getch();
if(con != 'y')
{
printf("\nType a different target file name: ");
gets(target);
}
}
ft = fopen(target, "w");
while((ch=fgetc(fs)) != EOF)
fputc(ch, ft);
printf("\nThe file has been copied successfully.");
fclose(fs);
fclose(ft);
getch();
return(0);
}
♣ Output ::
Type name of file to copy: muhit.txt Type name of target file: copy_of_muhit.txt The file has been copied successfully.
♣ Downloads ::
Comments
Post a Comment