I use a very convenient function (similar to PHP file_put_contents)
void filePutContents(const std::string& name, const std::string& content, bool append = false) {
std::ofstream outfile;
if (append)
outfile.open(name, std::ios_base::app);
else
outfile.open(name);
outfile << content;
}
When you need to add something simple:
filePutContents("./yourfile.txt","content",true);
Using this function, you do not need to worry about opening / closing. Altho should not be used in large cycles.
source
share