1

I am trying to add the contents of multiple files over to another file, but I am having trouble. It seems that every time I add the contents of one file, it overwrites the contents of the last file I added. Here is my code:

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

As always, I appreciate any help!

4

2 回答 2

5

You must use ios::app to configure the file stream in "append" mode.

using namespace std;
ofstream foo ("foo.bin", ios::out | ios::app | ios::binary);
于 2013-04-16T20:22:40.280 回答
1

is it append needed in binfile, I think the problem is in the myfile, i dont see any declaration here, but I suppose it is ofstream, and there should be ios::append

于 2013-04-16T20:30:13.213 回答