C-Programming

Method of copying the content of one file to another

 This program takes the contents of a text file and copies them into another text file character by character.

Example 2:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

{

char ch;

FILE *fs, *ft;

fs=fopen(“file1.txt”,”r”);

if(fs=NULL)

{

printf(“Cannot open source file”);

exit(1);

}

ft=fopen(“file2.txt”,”w”);

if(ft==NULL)

{

prntf(“Cannot open target file”);

fclose(fs);

exit(1);

}

while ((ch=fgetc(fs))!=EOF)

fputc(ch,ft);

fclose(fs);

fclose(ft);

getch();

}

In this program file1.txt is a source file which is opened in read mode and file2.txt is a destination file which is opened in write mode.

Character input/Output functions in file (Prev Lesson)
(Next Lesson) Using feof( ) function
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!