LazyCat
LazyCat

Reputation: 506

Handling map of files in c++

I need to write to a bunch of files simultaneously, so I decided to use map <string, ofstream>.

map<string, ofstream> MyFileMap; 

I take a vector<string> FileInd, which consists of, say "a" "b" "c", and try to open my files with:

for (vector<string>::iterator it = FileInd.begin(); iter != FileInd.end(); ++it){
    ...
    MyFileMap[*it].open("/myhomefolder/"+(*it)+".");
}

I get the error

request for member 'open' in ..... , which is of non-class type 'std::ofstream*'

I've tried to switch to

map<string, ofstream*> MyFileMap;

But it didn't work either.

Could anyone help?

Thanks.

Clarification:

I've tried both

map<string, ofstream> MyFileMap; map<string, ofstream*> MyFileMap;

with both

.open ->open

neither of 4 variants work.

Solution (suggested in Rob's code below):

Basically, I forgot "new", the following works for me:

map<string, ofstream*> MyFileMap;
MyFileMap[*it] = new ofstream("/myhomefolder/"+(*it)+".");

Upvotes: 9

Views: 9082

Answers (1)

Robᵩ
Robᵩ

Reputation: 168646

std::map<std::string, std::ofstream> can't possibly work, because std::map requires its data type to be Assignable, which std::ofstream isn't. In the alternative, the data type must be a pointer to ofstream -- either a raw pointer or a smart pointer.

Here is how I would do it, using C++11 features:

#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <vector>

int main (int ac, char **av)
{
  // Convenient access to argument array
  std::vector<std::string> fileNames(av+1, av+ac);

  // If I were smart, this would be std::shared_ptr or something
  std::map<std::string, std::ofstream*> fileMap;

  // Open all of the files
  for(auto& fileName : fileNames) {
    fileMap[fileName] = new std::ofstream("/tmp/xxx/"+fileName+".txt");
    if(!fileMap[fileName] || !*fileMap[fileName])
      perror(fileName.c_str());
  }

  // Write some data to all of the files
  for(auto& pair : fileMap) {
    *pair.second << "Hello, world\n";
  }

  // Close all of the files
  // If I had used std::shared_ptr, I could skip this step
  for(auto& pair : fileMap) {
    delete pair.second;
    pair.second = 0;
  }
}

and the 2nd verse, in C++03:

#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <vector>

int main (int ac, char **av)
{
  typedef std::map<std::string, std::ofstream*> Map;
  typedef Map::iterator Iterator;

  Map fileMap;

  // Open all of the files
  std::string xxx("/tmp/xxx/");
  while(av++,--ac) {
    fileMap[*av] = new std::ofstream( (xxx+*av+".txt").c_str() );
    if(!fileMap[*av] || !*fileMap[*av])
      perror(*av);
  }

  // Write some data to all of the files
  for(Iterator it = fileMap.begin(); it != fileMap.end(); ++it) {
    *(it->second) << "Hello, world\n";
  }

  // Close all of the files
  for(Iterator it = fileMap.begin(); it != fileMap.end(); ++it) {
    delete it->second;
    it->second = 0;
  }
}

Upvotes: 10

Related Questions