C program to copy contents of one file in to another file.

copyfile.cpp

#include <fstream>
#include <iostream>
int main()
{
 string line;
 ifstream ini_file{
 "original.txt"
 };
 ofstream out_file{ "copy.txt" };
 if (ini_file && out_file) {
 while (getline(ini_file, line)) {
 out_file << line << "\n";
 }
 cout << "Copy Finished \n";
 }
 else {
 printf("Cannot read File");
 }
 ini_file.close();
 out_file.close();
 return 0;
}


copyfile.c

#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp1,*fp2;
char ch;
clrscr();
fp1=fopen("abc.txt","r+");
fp2=fopen("xyz.txt","w+");
if(fp1==NULL || fp2==NULL){
printf("opration failed!!\n");
getch();
exit(0);
}
while(ch != EOF){
ch=fgetc(fp1);
fputc(ch,fp2);
}
getch();
fcloseall();
}

First create a file named "abc.txt" and write contents of it then save it in relative folder like BIN . After that run this program.

No comments:

Post a Comment