How to append text to a text file in C++? And create a new text file if it does not already exist and append text to it if it does exist.
Asked
Active
Viewed 4.1e+01k times
233
-
5http://cplusplus.com/reference/iostream/ofstream/open/ – bobobobo Mar 06 '10 at 17:40
5 Answers
372
You need to specify the append open mode like
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app); // append instead of overwrite
outfile << "Data";
return 0;
}

NathanOliver
- 171,901
- 28
- 288
- 402

Bertrand Marron
- 21,501
- 8
- 58
- 94
-
22No need to close the file manually, as it does so upon destruction. See http://stackoverflow.com/questions/748014/. Also,
is not being used in the example. – swalog Sep 12 '13 at 15:47 -
9
-
6Can use `std::ofstream::out | std::ofstream::app` instead of `std::ios_base::app`? http://www.cplusplus.com/reference/fstream/ofstream/open/ – Volomike Feb 25 '16 at 22:12
-
8You can also do more in the constructor if you want to cut down on code: std::ofstream outfile("test.txt", std::ios_base::app); – marsh Nov 03 '16 at 17:17
-
1You don't need to specify the `out` flag explicitly when using `std::ofstream`, it always uses the `out` flag implicitly for you. Same with the `in` flag for `std::ifstream`. You would have to specify the `in` and `out` flags explicitly if you were using `std::fstream` instead. – Remy Lebeau Nov 06 '19 at 22:31
-
Also instead of `std::ios_base::app` you can use shorter `std::ios::app`. – Arty Nov 05 '20 at 09:26
-
17
I use this code. It makes sure that file gets created if it doesn't exist and also adds bit of error checks.
static void appendLineToFile(string filepath, string line)
{
std::ofstream file;
//can't enable exception now because of gcc bug that raises ios_base::failure with useless message
//file.exceptions(file.exceptions() | std::ios::failbit);
file.open(filepath, std::ios::out | std::ios::app);
if (file.fail())
throw std::ios_base::failure(std::strerror(errno));
//make sure write fails with exception if something is wrong
file.exceptions(file.exceptions() | std::ios::failbit | std::ifstream::badbit);
file << line << std::endl;
}

Shital Shah
- 63,284
- 17
- 238
- 185
15
#include <fstream>
#include <iostream>
FILE * pFileTXT;
int counter
int main()
{
pFileTXT = fopen ("aTextFile.txt","a");// use "a" for append, "w" to overwrite, previous content will be deleted
for(counter=0;counter<9;counter++)
fprintf (pFileTXT, "%c", characterarray[counter] );// character array to file
fprintf(pFileTXT,"\n");// newline
for(counter=0;counter<9;counter++)
fprintf (pFileTXT, "%d", digitarray[counter] ); // numerical to file
fprintf(pFileTXT,"A Sentence"); // String to file
fprintf (pFileXML,"%.2x",character); // Printing hex value, 0x31 if character= 1
fclose (pFileTXT); // must close after opening
return 0;
}

Osaid
- 557
- 1
- 8
- 23
-
32
-
6
-
6@Osaid C is not a subset of C++. Compilers compile its code for backward compatibility. Many C-valid things are not C++-valid things, e.g. VLA. – stryku Feb 18 '18 at 14:00
-
1but if we want to append text in the middle of file? with C style? using FILE *?? "a+" or "a" with fseek() and ftell() didn't worked for me – vincent thorpe Mar 25 '19 at 19:49
2
You could use an fstream
and open it with the std::ios::app
flag. Have a look at the code below and it should clear your head.
...
fstream f("filename.ext", f.out | f.app);
f << "any";
f << "text";
f << "written";
f << "wll";
f << "be append";
...
You can find more information about the open modes here and about fstreams here.
2
You could also do it like this
#include <fstream>
int main(){
std::ofstream ost {outputfile, std::ios_base::app};
ost.open(outputfile);
ost << "something you want to add to your outputfile";
ost.close();
return 0;
}

vveil
- 321
- 1
- 9
-
1Passing the filename to the `ofstream` constructor opens the file immediately, so calling `open()` afterwards is redundant. – Remy Lebeau Nov 06 '19 at 22:32
-
also `ost.close();` is redundant as it's called in the `std::ofstream` destructor. – Mohamed Kandeel Apr 06 '21 at 10:26