2013-04-16 62 views
1

我想將多個文件的內容添加到另一個文件中,但我遇到了問題。似乎每次添加一個文件的內容時,它都會覆蓋我添加的最後一個文件的內容。這裏是我的代碼:將來自多個文件的信息添加到一個文件中C++

cout << "Enter Directory Location" << endl; 
    string name; 
    getline(cin, name); 
    cout << "Directory: " << name << " Used" << endl; 
    name += "/title"; 

    int x = 1; // I am assuming that the file number will simply start at 1 
    int y; 
    cout << "Enter Number of Files" << endl; 
    cin >> y; 

    while(x <= y) 
    { 
     stringstream sstm; 
    sstm << name << x; 
    name = sstm.str(); 
    name += ".png"; 
    ifstream binfile(name.c_str(),ios::in | ios::binary); 

     myfile << binfile.rdbuf(); 
    x++;  
    } 

一如既往,我感謝任何幫助!

+10

IOS: :追加,也許? –

回答

5

您必須使用ios::app以「附加」模式配置文件流。是將其追加需要binfile

using namespace std; 
ofstream foo ("foo.bin", ios::out | ios::app | ios::binary); 
+0

-1:OP附加到'myfile',它是一個輸出流,沒有'ios :: append'。 –

+0

將變量的名稱更改爲myFile和binfile,以避免模糊重要的位。 –

+0

這仍然是錯誤的。 –

1

,我認爲這個問題是在myfile的,我沒有看到這裏的任何聲明,但我想這是ofstream和應有的ios ::追加

相關問題