Write Fstream
c++ : Write to text file is overwriting then editting?
include
#include
using namespace std;
int main()
{
ofstream out("example.txt");
if(!out) {
cout << "Cannot open file.n";
return 1;
}
out << 10 << " " << 123.23 << endl << endl;
out << "This is a short text file.";
out.close();
return 0;
}
can anyone tell me why this program is OVERWRITING my file?
rather then editting?
The single parameter version of out will clear the file before writing to it. To append to an existing file, you need to specify an additional parameter to specify the mode.
ofstream out("example.txt", ios::app);
How to Write to a Text File in C++
Compare............................................................................................
|
|
Write $28.99 Write - Wood Sign |
|
|
Write Is Wrong $7.99 Write Is Wrong |
.........................................................................................................