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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: