Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Write to an existing file? [duplicate]

Tags:

c++

In C++, I need to write to an existing file and keep the previous content there.

This is what I have done:

std::ofstream logging;

logging.open(FILENAME);

logging << "HELLO\n";

logging.close();

but then my previous text is overwritten (gone). What did I do wrong?

Thanks in advance.

like image 355
olidev Avatar asked Nov 15 '10 16:11

olidev


2 Answers

logging.open(FILENAME, std::ios_base::app); 
like image 155
Alexey Malistov Avatar answered Sep 19 '22 23:09

Alexey Malistov


You have to open the file in append mode:

logging.open(FILENAME, std::ios::app);
like image 23
Drew Frezell Avatar answered Sep 21 '22 23:09

Drew Frezell