
c++怎样实现数据存入指定的TXT文档
1个回答
展开全部
1、类似于以下代码:
2、需要<fstream>头文件
3、主要是ofstream类的对象的操作。
void CNews::save(string fileName)
{
ofstream outfile(fileName.c_str());
string ar;
if(!outfile)
{
cerr << "error:unable to open input file:" << outfile << endl;
return;
}
//format_save
for(mapIter it=newsGroup.begin();it!=newsGroup.end();it++)
{
outfile << it->first
<< endl
<< it->second.first
<< endl;
for(vector<string>::iterator vIt=it->second.second.begin();vIt!=it->second.second.end();vIt++)
outfile << *vIt;
outfile << endl;
}
outfile.close();
}
4、理所当然地可以缩减成以下代码,上述的是我以前写的类的文件保存函数
#include<fstream>
using namespace std;
int main()
{
ofstream outfile("filename.txt");
if(!outfile)
{
cerr << "error:unable to open input file:" << endl;
return;
}
outfile << "C++ program .file version." << endl;
outfile.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询