Program to Copy Content of One File into Another File
We already know how to open a file, read contents of a file and write into a file. So in this program, we will read from one file and simultaneously write into the other file, till we reach end of first file.
#include<stdio.h> void main() { /* File_1.txt is the file with content and, File_2.txt is the file in which content of File_1 will be copied. */ FILE *fp1, *fp2; char ch; int pos; if ((fp1 = fopen("File_1.txt","r")) == NULL) { printf("\nFile cannot be opened"); return; } else { printf("\nFile opened for copy...\n "); } fp2 = fopen("File_2.txt", "w"); fseek(fp1, 0L, SEEK_END); // file pointer at end of file pos = ftell(fp1); fseek(fp1, 0L, SEEK_SET); // file pointer set at start while (pos--) { ch = fgetc(fp1); // copying file character by character fputc(ch, fp2); } fcloseall(); }
To learn about File Handling in C, Visit the studytonight Lesson : File Handling in C
No comments:
Post a Comment