2010-11-26 6 views
0

텍스트 파일을 다른 텍스트 파일로 복사하는 방법은 무엇입니까? 여기에 내 현재 진행 .. 그냥 소스 파일 :(의 마지막 줄을 복사텍스트 파일 내용을 다른 텍스트 파일로 복사

 ifstream stream1("c:\\source.txt"); 

     char a[512]; 


     while(!stream1.eof()) 

     { 
      stream1 >> a; 
     } 


    ofstream myfile; 
    myfile.open ("c:\\destination.txt"); 
    myfile << a; 
    myfile.close(); 

답변

4

간단한 해결책 :

ifstream stream1("C:/source.txt"); 
ofstream stream2("C:/target.txt"); 
stream2 << stream1.rdbuf(); 
1

이 뒤에 그 이유는 a 당신이에 작성하는 문자 배열이 아니라 스트림입니다. 그래서 모든 루프되고있는 . 메모리에 동일한 위치 (대신 추가하지 않음)

당신은 단지 시작에 ofstream 개체를 만들고 스트림 스트림에서 작성할 수

#include <fstream> 

void main() 
{ 
    std::ifstream stream1("C:\\source.txt"); 
    std::ofstream stream2("C:\\target.txt"); 

    while(!stream1.eof()) 
     stream2 << stream1; 

    stream1.close(); 
    stream2.close(); 
} 
+0

여기에 필요한 특정 헤더 파일은 무엇입니까? 내가 컴파일했지만 오류가 발생했습니다 ... – karikari

+0

오류 메시지는 다음과 같습니다 : 오류 오류 C2679 : 이진 '>>': 'std :: ofstream'유형의 오른쪽 피연산자를 사용하는 연산자가 없습니다 (또는 허용되지 않습니다 변환) \t c : \ test.cpp – karikari

+1

그냥 - 스트림을 while 루프 내에서 스왑해야하지만. 답변 업데이트 ... 오류가 발생하면 방향을 바꿔보십시오. – Mario

관련 문제