Wie füge ich Text an eine Textdatei in C ++ an? Erstellen Sie eine neue Textdatei, falls diese noch nicht vorhanden ist, und hängen Sie Text an, falls vorhanden.
Wie füge ich Text an eine Textdatei in C ++ an? Erstellen Sie eine neue Textdatei, falls diese noch nicht vorhanden ist, und hängen Sie Text an, falls vorhanden.
Antworten:
Sie müssen den Append Open-Modus wie angeben
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app); // append instead of overwrite
outfile << "Data";
return 0;
}
std::ofstream::out | std::ofstream::app
anstelle von verwenden std::ios_base::app
? cplusplus.com/reference/fstream/ofstream/open
out
Flag bei der Verwendung nicht explizit angeben std::ofstream
, es verwendet das out
Flag immer implizit für Sie. Gleiches gilt für die in
Flagge für std::ifstream
. Sie müssten die Flags in
und out
explizit angeben, wenn Sie std::fstream
stattdessen verwenden würden.
Ich benutze diesen Code. Es stellt sicher, dass die Datei erstellt wird, wenn sie nicht vorhanden ist, und fügt auch einige Fehlerprüfungen hinzu.
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;
}
#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;
}
Sie könnten es auch so machen
#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;
}
ofstream
Konstruktor übergeben, wird die Datei sofort geöffnet, sodass ein anschließender Aufruf open()
überflüssig ist.
Ich habe meinen Code für die Antwort aus einem Buch namens "C ++ Programming In Easy Steps" erhalten. Das könnte unten funktionieren.
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
ofstream writer("filename.file-extension" , ios::app);
if (!writer)
{
cout << "Error Opening File" << endl;
return -1;
}
string info = "insert text here";
writer.append(info);
writer << info << endl;
writer.close;
return 0;
}
Ich hoffe das hilft dir.
Sie können ein verwenden fstream
und es mit der std::ios::app
Flagge öffnen . Schauen Sie sich den Code unten an und er sollte Ihren Kopf frei machen.
...
fstream f("filename.ext", f.out | f.app);
f << "any";
f << "text";
f << "written";
f << "wll";
f << "be append";
...
Sie können weitere Informationen zu den offenen Modi finden hier und über fstreams hier .