比如说有一个有文本的txt文件,我要在文本中间添加新文本,怎么做才能不覆盖原来的文本?
http://wenwen.sogou.com/z/q553022171.htm
http://bbs.csdn.net/topics/390533611
这里有两个链接你看看对你有没有帮助
文件追加,append
以append方式打开
如果是想在一段内容中插入新的内容,就必须先保留插入位置以后的内容,然后插入新内容,然后再将保存的内容写入
#include <iostream>
#include <fstream>
using namespace std;
ofstream outfile;
void writeLog()
{
outfile.open("C:\\myfile.txt", ios::app);
if(!outfile) //检查文件是否正常打开
{
cout<<"abc.txt can't open"< abort(); //打开失败,结束程序
}
else
{
outfile << "world" << endl;
outfile.close();
}
}
void writeLog2()
{
outfile.open("C:\\myfile.txt", ios::app);
if(!outfile) //检查文件是否正常打开
{
cout<< "abc.txt can't open"<< endl;
abort(); //打开失败,结束程序
}
else
{
outfile << "Write log2" << endl;
outfile.close();
}
}
int main(int argc, char* argv[])
{
writeLog();
writeLog2();
printf("Hello World!\n");
return 0;
}