In this program user will take command to copy the content from one file to other file with the help of file handling concept. As required user declare the variables for function regarding here user must have to create a file with content in it for copying as user set the control statement that if (args!=3) then insufficient arguments message will be on the screen. In next move user made a condition if (fp1==Null) then the existing file can’t be open and the targeted file also been neglected by the control statement. Then awhile condition comes in lime light for vary the condition to copying the file. While (ch!=EOF) until the character not been checked to end of file. Put the content of fp1 to targeted place. And then a message appears file copied on the screen as result.
Problem Statement:
This is C Program that asks user to copy file from one to other file using file handling concept.
- Declaring the variables.
- Using control statement.
- Elaborate the while condition to function.
- Display the result on the screen.
Here is C source code for Copy file from one to other file using file handling concept. The output of this program shown below.
#include<stdio.h>
#include<process.h>
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char ch;
clrscr();
if(argc!=3)
{
printf("Insufficient Arguments");
exit(0);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("Source File can't be Opened");
exit(0);
}
fp2=fopen(argv[2],"w");
if(fp2==NULL)
{
printf("Target File can't be Opened");
fclose(fp1);
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
fputc(ch,fp2);
ch=fgetc(fp1);
}
printf("File Copied");
fclose(fp1);
fclose(fp2);
}