如何在C ++中将文本追加到文本文件?

如何在C ++中将文本追加到文本文件?如果不存在则创建新的,如果存在则追加。


慕姐8265434
浏览 759回答 3
3回答

蛊毒传说

#include <fstream>int main() {&nbsp;&nbsp;&nbsp; std::ofstream outfile;&nbsp; outfile.open("test.txt", std::ios_base::app);&nbsp; outfile << "Data";&nbsp;&nbsp; return 0;}

GCT1015

#include <fstream>&nbsp;#include <iostream>&nbsp;FILE * pFileTXT;&nbsp;int counterint main(){&nbsp;pFileTXT = fopen ("aTextFile.txt","a");// use "a" for append, "w" to overwrite, previous content will be deleted&nbsp;for(counter=0;counter<9;counter++)&nbsp;fprintf (pFileTXT, "%c", characterarray[counter] );// character array to file&nbsp;fprintf(pFileTXT,"\n");// newline&nbsp;for(counter=0;counter<9;counter++)&nbsp;fprintf (pFileTXT, "%d", digitarray[counter] );&nbsp; &nbsp; // numerical to file&nbsp;fprintf(pFileTXT,"A Sentence");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// String to file&nbsp;fprintf (pFileXML,"%.2x",character);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Printing hex value, 0x31 if character= 1&nbsp;fclose (pFileTXT); // must close after opening&nbsp;return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP