如何将文本附加到 C++ 中的文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2393345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append text to a text file in C++?
提问by Ahmad Farid
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.
如何将文本附加到 C++ 中的文本文件?如果尚不存在,则创建一个新文本文件,如果存在,则将文本附加到其中。
回答by Bertrand Marron
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;
}
回答by Shital Shah
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;
}
回答by Osaid
#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;
}
回答by vveil
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;
}
回答by Tom
I got my code for the answer from a book called "C++ Programming In Easy Steps". The could below should work.
我从一本名为“C++ Programming In Easy Steps”的书中得到了答案的代码。下面的可能应该工作。
#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;
}
I hope this helps you.
我希望这可以帮助你。
回答by ranu
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.
您可以使用 anfstream
并用std::ios::app
标志打开它。看看下面的代码,它应该会让你清醒。
...
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 hereand about fstreams here.