我到目前为止,还不知道具体的如何通过C++打开一个文件。下面就是一个问题,求指导。
这我是想把一个关联容器中的每个pair输入到一个.txt文档里,但是就是失败。运行不了。求大神帮忙:#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<map>
using namespace std;
fstream& open_file(fstream& out,const string& file)
{
out.close();
out.clear();
out.open(file.c_str());
return out;
}
int main()
{
fstream text1;
string s("C:Users\Zhchen\Desktop\text1.txt");
open_file(text1,s);
map<string,string> mapfile;
mapfile["'em"]="them";
mapfile["cuz"]="because";
mapfile["i"]="I";
mapfile["nah"]="no";
mapfile["pos"]="supposed";
mapfile["sez"]="said";
mapfile["tanx"]="thanks";
mapfile["wuz"]="was";
map<string,string>::iterator map_it=mapfile.begin();
if(!text1)
throw runtime_error("no aim file!!");
while(map_it!=mapfile.end())
{
cout<<map_it->first<<"\t"<<map_it->second<<"\n";
text1<<map_it->first<<"\t"<<map_it->second<<"\n";
map_it++;
}
text1.close();
int end;
cin>>end;
return 0;