You are here:

C++/How to apped data at enf of text file



I have a question. I know how to read text files using ifstream. Now I would like to append some data at the end of the file.

I read a text file as follows:

#include "stdafx.h"
using namespace std;

void main()
  string line;
  ifstream test_file;"test.txt", ios::in);
        getline(test_file, line);

Now I would like to append some more data to the end of the text file. The data are numbers(int).

How can I do that.

Please Help.


That basically depends on exactly what you wish to do.

If you have only opened the file and read all the contents so as to position at the end of the file so you can add new data and are not really interested in reading the existing data then open the file for _writing_ (i.e. output) using std::ofstream or std::fstream. However opening using std::ofstream usually empties a file of all the exiting data (or creates a new empty file if one does not already exist) - so you have to specify a non-default set of open flags directly thus:

   std::fstream out("test.txt", std::ios::out | std::ios::app);


   std::fstream out;"test.txt", std::ios::out | std::ios::app);

I have used std::fstream rather than std::ofstream. The only difference between std::fstream and std::ofstream and std::ifstream is that std::ofstream opens files using std::ios::out by default and std::ifstream opens files using std::ios::in by default (so your explicit qualification of this open mode flag in the posted code was redundant).

So as I have had to specify an explicit set of open mode flags I chose to use std::fstream. The std::ios::app used with std::ios::out means open for writing, appending data to the end of the file and creating the file if necessary. Note that the | operator is the bitwise OR operator.

On the other hand if you do need to read the existing data in the file before appending the new data then open the file for reading and writing:

   std::fstream out("test.txt", std::ios::in | std::ios::out);


   std::fstream out;"test.txt", std::ios::in | std::ios::out);

This combination of open mode flags opens the file for both reading and writing, leaving the initial file position at the start of the file. As with std::ios::in the file must exist.

There are probably several references and tutorials for the C++ IOStreams library online somewhere. I located this reference material:, which seems reasonably accurate (at least for file stream open modes), if a little terse - it is a reference rather than a tutorial after all.

Hope this helps  


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.