How can I copy a text file into another? I tried this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
ofstream outfile("output.txt");
outfile << infile;
return 0;
}
This just ends up leaving the following value in output.txt
: 0x28fe78
.
What am I doing wrong?
You can save the content of the infile in a string and put it out into the other file. Try this:
#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 regards Paul
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다