open file is very expensive operation. you should store somewhere already opened streams. for example you can use std::map< std::string, boost::shared_pre< std::ofstream > > for this purpose