728x90 AdSpace

Trending

Copy content of file to another File in c

make use of fopen function to open a file, which is in stdio.h library.
FILE *fopen(const char *filename, const char *mode);


mode Description
"r" Open a file for reading. The file must exist.
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is considered as a new empty file.
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing.
"a+" Open a file for reading and appending.
/*
author: ba-programmer.blogspot.com
program:- coping content of one file to another file in c
*/
#include
int main()
{
char ch, filename[20];
FILE *source, *dest;

printf("Enter name of file to copy : ");
scanf(" %s",filename);//opening file in read mode
source = fopen(filename, "r");

if( source == NULL )
{
printf("FileNotFound");
exit(0);
}

printf("Enter name of file to which content should be copied : ");
scanf(" %s",filename);

dest = fopen(filename, "w");//opening file in write mode

if( dest == NULL )
{
fclose(source);
printf("Error try again");
exit(0);
}

while( ( ch = fgetc(source) ) != EOF )
fputc(ch, dest);//reading from source and writing to destination file

printf("File copy Operation successfully.\n");

fclose(source);
fclose(dest);//closing files

return 0;
}
Stay connected...!
comment box is waiting for your opinion...!
Copy content of file to another File in c Reviewed by Unknown on 07:40 Rating: 5 make use of fopen function to open a file, which is in stdio.h library. FILE *fopen(const char *filename, const char *mode); mode Desc...

2 comments: