minusatwelfth
minusatwelfth

Reputation: 191

open and write to existing fstream without overwrite/clearing in C++

I have an existing txt file, I want to be able to write stuff to it (on the next line) without modifying the existing contents. What's a good way to do this?

#include <iostream>
ofstream myfile ("ex.txt"); 

seems to clear all the existing text.

Upvotes: 1

Views: 5100

Answers (2)

mathematician1975
mathematician1975

Reputation: 21351

Open the file using the ios_base::app flag

Upvotes: 1

jahhaj
jahhaj

Reputation: 3119

ofstream myfile ("ex.txt", ios_base::app); will open a file in append mode. Each write operation will append to the existing contents.

Upvotes: 2

Related Questions