How can I copy a text file to another?
How can I copy a text file to another? I've tried this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
outfile << infile;
return 0;
}
It finishes with the fact that reserves in output.txt
the following meaning: 0x28fe78
.
What am I doing wrong?
+3
source to share
1 answer
You can store the content of infile on a line and output it to another file. Try the following:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
string content = "";
int i;
for(i=0 ; infile.eof()!=true ; i++) // get content of infile
content += infile.get();
i--;
content.erase(content.end()-1); // erase last character
cout << i << " characters read...\n";
infile.close();
outfile << content; // output
outfile.close();
return 0;
}
Best wishes Pavel
+7
source to share