I need to write to a bunch of files at the same time, 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 an error
request for member 'open' in ..... , which is of non-class type 'std::ofstream*'
I tried to switch to
map<string, ofstream*> MyFileMap;
But that didn't work either.
Can anyone help?
Thanks.
Clarification:
I tried both
map<string, ofstream> MyFileMap;
map<string, ofstream*> MyFileMap;
with both
.open
->open
none of the 4 options work.
Solution (proposed in Rob code below):
Basically, I forgot the βnewβ, the following works for me:
map<string, ofstream*> MyFileMap; MyFileMap[*it] = new ofstream("/myhomefolder/"+(*it)+".");
source share