I’m having troubles with ofstream , which is – when I change value of ofstream object like this
ofstream o_save;
/*code*/
o_save = ofstream(filename); //Problem is here
…the line above completely erase contents of file.
The question is how do I legitimately change filename value of ofstream object ?
P.S. I cant define it when I declare it, because I want it global and I’m not sure which save file I select.
The question is quite vague and contradictory, and the OP seems to have slept after asking the question. So I shall try to peek inside his head and try to elaborate what he wants.
For opening a file, there are many modes for that. Open it like this.
If you want to preserve the original contents of that file, use..
If you want to close it later and open another one, close using…
Chaning of file names is normally not allowed in case of
ofstream. You can userenamefrom<cstdlib>. You can delete a file though usingremove("file.txt")in<cstdio>.