C++ program to copy a text file to another text file

3 comments
/* “fstream" class is used for both reading and writing a file. So we need to copy the contents of a file into another file than we will use “fstream”, it has functions capable of both reading & writing in the file. NOTE: both the text file has to be present initially. */

Code:
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main ()
{
fstream a,b;
char ch;
a.open("bunty.txt");
//The file from which the content will be copied
b.open("babli.txt"); //The file to which the content will be copied
while (!a.eof())
{
a.get(ch);
//reading from file object 'a'
cout<<ch;
b<<ch;
//writing to file babli.txt
}
a.close();
b.close();
return 0;
}



3 comments:

AHAMMED SHAMIL K said...

this program working copy. but output file ended with a ÿ sign. why?

AHAMMED SHAMIL K said...

this program working copy. but output file ended with a ÿ sign. why?

Vibhutesh Kumar Singh said...

Hi. This happen if there is a mismatch in encoding schemes that is used to write and read the text file.