Write a C program to copy the contents of one file to another.
Algorithm:
1. Start
2. read command line arguments
3. check if no of arguments =3 or not. If not print invalid no of arguments
4. open source file in reading mode
5. if the NULL pointer, then print source file cannot be open
6. open destination file in write mode
7. if NULL pointer, then print destination file cannot be open
8. read a character from source file and write to destination file until EOF
9. Close source file and destination file
10. Stop
Program Code:
#include<stdio.h>
void main()
{
FILE *fp1, *fp2;
char ch;
fp1=fopen("old.txt","r");
fp2=fopen("new.txt","w");
if(fp1==NULL||fp2==NULL)
{
printf("file opening problem");
return;
}
while(!feof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
getch();
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: