我想打开多个文本文件并将流存储为 vector 。
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
vector<string> imgSet
vector<ofstream> txtFiles;
// .
// .
for( int i=0 ; i<imgSet.size() ; i++ )
{
ofstream s;
s.open( imgSet[i].getName(), std::ofstream::out );
txtFiles.push_back( s );
}
}
getName 看起来像:
const string& getName() const;
我在 ubuntu 上用 G++ 编译这个,我不明白为什么我会得到一长串错误。如何解决这个问题
最佳答案
C++03 中的 std::fstream 中没有 operator= 或复制构造函数。 你可以这样做:
vector<ofstream*> txtFiles;
//...
for( int i=0 ; i<imgSet.size() ; i++ )
{
txtFiles.push_back( new ofstream(imgSet[i].getName(), std::ofstream::out) );
}
关于c++ - 打开流的多个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058896/