Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ofstream to write text to the end of a file

Tags:

c++

ofstream

How do I use the ofstream to write text to the end of a file without erasing its content inside?


2 Answers

You can pass the flag ios::app when opening the file:

ofstream ofs("filename", ios::app);
like image 85
Konrad Rudolph Avatar answered Nov 20 '25 11:11

Konrad Rudolph


You want to append to the file. Use ios::app as the file mode when creating the ofstream.

Appending will automatically seek to the end of the file.

like image 32
Ed Haber Avatar answered Nov 20 '25 11:11

Ed Haber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!