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

Add information from multiple files into one file C++

本文关键字:文件 一个 信息 添加      更新时间:2023-10-16

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

  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++;    
  }
一如既往,我感谢任何帮助!

必须使用ios::app将文件流配置为"append"模式

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

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