Miscellaneous › Others › Programs related to file handling in C language. › /*simple program of file
March 25, 2013 at 7:01 am
#9370
Arjun Vaghani
Participant
/*simple program of file Handling*/
/*copy content of 1 file to another*/
#include<stdio.h>
main(int argc, char **argv)
{
char ch;
FILE *fp1,*fp2;
if(argc!)
{
printf(“Invalid syntax: use: ./cpy <source_filename> <destinatin_filename>n”);
return;
}
fp1=fopen(argv[1],”r”);
if(fp1==NULL)
{
printf(“source file does not existsn”);
return;
}
fclose(fp1);
fp2=fopen(argv[2],”r”);
if(fp2!=NULL)
{
here: printf(“Ur file already exists..Do u want to overwrite? press y for yes and n for no:”);
scanf(“%c”,&ch);
if(ch==’n’||ch==’N’)
return;
else if(ch!=’y’&& ch!=’Y’)
{
printf(“Invalid choicen”);
goto here;
}
fclose(fp2);
while((ch=getchar())!=’n’); /*for clearing buffer*/
}
fp1=fopen(argv[1],”r”);
fp2=fopen(argv[2],”w”);
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
}