小编vah*_*olf的帖子

c ++中Ofstream的数组

我希望在我的项目中使用41个输出文件来在其上写入文本.首先创建一个字符串数组list来命名那些输出文件然后我试图定义一个ofstream对象的数组并用它list来命名它们,但是我得到了这个错误'outfile' cannot be used as a function.以下是我的代码:

#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
using namespace std ;
int main ()
{
  string list [41];
  int i=1;
  ofstream *outFile = new ofstream [41];

  for (i=1;i<=41 ;i++)
  {
    stringstream sstm;
    sstm << "subnode" << i;
    list[i] = sstm.str();
  }

  for (i=0;i<=41;i++)
    outFile[i] (list[i].c_str());

  i=1;
  for (i=1;i<=41;i++)
    cout << list[i] << endl;

  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays ofstream

4
推荐指数
1
解决办法
8270
查看次数

标签 统计

arrays ×1

c++ ×1

ofstream ×1