You are here:

C++/I/O text file


I wont to add some extra information (text or int) to an existence text file with out deleting the previous information.

You do not say how you are currently trying to open you files. However as you are asking a C++ expert in the C++ question area I shall concentrate more on C++ IOStreams.

The problem (for C or C++ file I/O) is that you are opening the file for writing with truncation - which is the default mode for writing (e.g. it is the default mode for C++ std::ofstream objects, and is the effect of the passing a mode string of "w" to the C library fopen function). Truncation means that if the file exists then any previous contents are cleared when the file is opened and the new data overwrites what was in the file before - as if you had deleted the file then re-created it.

Instead you have to open the file for write-appending. You do this by explicitly supplying the open mode to a C++ file stream object either when creating it or when calling its open operation e.g.:

   std::fstream out("path/to/file/the_file", std::ios::out | std::ios::app );


   std::fstream out;"path/to/file/the_file", std::ios::out | std::ios::app );

The default mode flags for std::ofstream are std::ios::out, which is the same as std::ios::out | std::ios::trunc. Note that it is _only_ in the default open modes that std::ofstream and std::ifstream differ. They differ from std::fstream in that they have default open modes at all!

For the C library fopen function use a mode flag of "a" (append) rather than "w".  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.