TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
File Handling: Copy Content of One File to Another in C
Mr Shanawar
Jan 25
2016
Code
1.4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
/*
Name: SHANAWAR
Copyright:
Description: PROGRAM THAT READS DATA FROM ONE FILE AND WRITE IT ON ANOTHER FILE.
*/
#include<stdio.h>
#include<conio.h>
int
main()
{
FILE
*fr,*fw;
// file pointers
char
fname[20],fname2[20],str[80];
printf(
"Source file name"
);
gets(fname);
// fopen( file_name , mode)
if
( ( fr = fopen(fname,
"r"
) ) == NULL )
// "r" represents read mode
{
// if fr is equals to NULL it means file can't open
printf(
"File Can't Open"
);
return
1;
}
printf(
"Enter Target file name"
);
gets(fname2);
if
( ( fw = fopen(fname2,
"a"
) ) == NULL)
// "a" represents append mode
{
// if fw is equals to NULL it means file can't open
printf(
"File Can't Open"
);
return
1;
}
while
( fgets(str,80,fr) != NULL )
// reading strings from source file
{
fputs(str,fw);
// writing strings into target file
}
// close opened files
fclose(fr);
fclose(fw);
getch();
return
0;
}
Copy Content of One File to Another.C
File handling