Handling map of files in c++

前端 未结 1 1029
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 18:50

I need to write to a bunch of files simultaneously, so I decided to use map .

map MyFileMap; 


        
相关标签:
1条回答
  • 2020-12-20 19:20

    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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题