将多个文件中的信息添加到一个文件C ++中

时间:2013-04-16 20:04:54

标签: c++ file copy

我正在尝试将多个文件的内容添加到另一个文件中,但我遇到了麻烦。似乎每次添加一个文件的内容时,它都会覆盖我添加的最后一个文件的内容。这是我的代码:

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

与往常一样,我感谢任何帮助!

2 个答案:

答案 0 :(得分:5)

您必须使用ios::app以“追加”模式配置文件流。

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

答案 1 :(得分:1)

是否在binfile中需要追加,我认为问题出在myfile中,我在这里看不到任何声明,但我认为它是ofstream,并且应该有ios :: append