Search code examples
c++vectorfstream

Opening multiple text files ofstream


I want to open multiple text files and store the streams as a 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 looks like :

const string& getName() const;

I am compiling this with G++ on ubuntu, I don't understand why I get a long list of errors with it. How can this be fixed


Solution

  • There is no operator= or copy constructor in std::fstream in C++03. You can do this:

    vector<ofstream*> txtFiles;
    //...
    for( int i=0 ; i<imgSet.size() ; i++ )
    {
            txtFiles.push_back( new ofstream(imgSet[i].getName(), std::ofstream::out) );
    }