2017-11-17 2 views
-1

.txt 파일에 출력이없는 것 같습니다. 나는 미래에 텍스트 파일 아래에 추가하고 싶습니다.내 txt 파일에 글을 쓰고 아래에 텍스트를 추가하는 방법

std::ofstream myfile(filename.str(), std::ofstream::out | std::ofstream::app); 

그리고 너무 그 파일을 엽니 다 ofstream의 객체를 생성하는 동안 파일 이름을 통과, 다시 .open를 호출 할 필요가 없습니다으로

using namespace std; 

int main() 
{ 
    string month; 
    int year; 
    stringstream filename; 

    cin >> month; 
    cin >> year; 

    filename << "Expenses_" << month << "_" << year << ".txt"; 
    ofstream myfile(filename.str()); 

    myfile.open(filename.str()); 
    myfile << "Hello World!"; 
    myfile.close(); 

    return 0; 
} 
+0

[모든 좋은 초보자의 책] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) 정보를 가지고 있어야 그것에 대해. [C++ 입출력 시스템에 대한 좋은 참고서] (http://en.cppreference.com/w/cpp/io)도 도움이 될 것입니다. –

+0

http://www.cplusplus.com/reference/fstream/ofstream/open/ – macroland

답변

0

은 당신의 파일을 엽니 다.

0
ofstream myfile(filename.str(), ofstream::out | ofstream::app); 

는 생성자가 자동으로을 작성하기위한 파일 을 열고 파일의 끝에 기록 포인터 이동, 그래서 당신은 파일에 추가 할 수 있습니다. std::ofstream(const char*, int) 구성 자 이 이미 파일을 열었 기 때문에 파일을 다시 열 필요가 없습니다.

대안은 다음과 같습니다

ofstream myfile; 
myfile.open(filename.str(), ofstream::out | ofstream::app); 
관련 문제