c++ - 打开流的多个文本文件

标签 c++ vector fstream

我想打开多个文本文件并将流存储为 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/

相关文章:

c++ - tbb::parallel invoke 仅执行一次函数

c++ - 使用 `const reference` 在显式构造函数中分配 `std::vector` 成员不是持久的

c++ - 如何在 Visual Studio 中打开 fstream 进行读写操作

c++ - 函数不写入 txt 文件

python - Arduino 到 Raspberry crc32 检查

c++ - 无法实现光子映射

c++ - 对c++ vector 的交集

vector - rust 是否可以为不同的数据结构提供统一的 Iterator 接口(interface)?

c++ - 在 C++ 中将信息写入文件的函数

c++ - Boost日志不写入文件,怎么办?