Come aggiungere testo a un file di testo in C++? Crea nuovo se non esiste e aggiungi se esiste.Come aggiungere testo a un file di testo in C++?
risposta
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app);
outfile << "Data";
return 0;
}
Non c'è bisogno di chiudere il file manualmente, come lo fa su di distruzione. Vedi http://stackoverflow.com/questions/748014/. Inoltre, nell'esempio non viene utilizzato
Puoi usare ios :: app al posto di ios_base :: app –
Puoi usare 'std :: ofstream :: out | std :: ofstream :: app' invece di 'std :: ios_base :: app'? http://www.cplusplus.com/reference/fstream/ofstream/open/ – Volomike
#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;
}
Io uso questo codice. Si assicura che il file venga creato se non esiste e aggiunge anche bit di controllo degli errori.
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;
}
- 1. aggiungere testo o dati in file di testo a Swift
- 2. Come aggiungere testo a un file in golang?
- 3. Aggiungere testo a ggplot
- 4. Impossibile aggiungere testo in un file
- 5. Come aggiungere testo a un file su una nuova riga
- 6. C#: aggiunta * del contenuto * di un file di testo in un altro file di testo
- 7. Come aggiungere automaticamente testo a testo copiato con JavaScript
- 8. Aggiungere punti elenco di testo a un modulo C#
- 9. Come aggiungere un file di testo in Android?
- 10. Aggiungere testo a CALayer
- 11. Come applico il testo a un file?
- 12. Come aggiungere testo in una casella di testo multilinea?
- 13. Aggiunta di testo a un file immagine
- 14. come aggiungere un bordo a un testo in textview android
- 15. Come leggere un file di testo in crescita in C++?
- 16. come aggiungere testo con formato in testo di modifica?
- 17. Come aggiungere testo in CodeMirror
- 18. Come creare un file di testo temporaneo in C++?
- 19. In CKEditor, come posso aggiungere un'etichetta "testo" a un pulsante?
- 20. Aggiungere testo a ogni riga nel file di testo utilizzando PowerShell
- 21. Come analizzare un file di testo in Objective-C?
- 22. Come aggiungere file di testo utilizzando file batch
- 23. Come aggiungere il testo dell'etichetta a DetailDisclosureButton?
- 24. C# - Carica un file di testo come classe
- 25. Esportare un DataSet C# in un file di testo
- 26. Come aggiungere un'animazione a una vista di testo in Android
- 27. Come aggiungere un'etichetta di testo semplice a un'immagine in Go?
- 28. Aggiungere un evento click a un testo in ios NSString
- 29. Come aggiungere una nuova riga di testo a un file esistente in Java?
- 30. Analisi efficiente di un grande file di testo in C#
http://cplusplus.com/reference/iostream/ofstream/open/ – bobobobo