2014-10-10 3 views
3

텍스트 파일을 다른 파일로 복사하려면 어떻게해야합니까?텍스트 파일을 다른 파일로 복사하는 방법은 무엇입니까?

#include <iostream> 
#include <fstream> 

using namespace std; 

int main() 
{ 
    ifstream infile("input.txt"); 
    ofstream outfile("output.txt"); 
    outfile << infile; 

    return 0; 
} 

이것은 단지 output.txt에 다음 값을 떠나는 끝 : 0x28fe78이 시도.

내가 뭘 잘못하고 있니?

+2

당신이 입력 파일에서 읽을 필요, 출력 파일 – taocp

+0

로 쓰기'<< infile'은 내부 파일 핸들 식별자를 작성하는 것입니다. 예 : 당신이 코드를 실행할 때,'input.txt' 핸들은 id가 0x28fe78이었습니다. –

+1

대신'outfile << infile.rdbuf();를 시도하십시오. –

답변

6

infile의 내용을 문자열에 저장하고 다른 파일에 저장할 수 있습니다. 이 시도 :

#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; 
} 

안부 폴

관련 문제