c++ 单词转换程序
单词转换文件内容:em them
cuz because
gratz grateful
i I
nah no
pos suppose
sez said
tanx thanks
wuz was
需要转换的文件内容:nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
程序输出的结果:no I said thanks beause I was supposed to
not beause I was grateful
程序:
//一个翻译程序
#include<iostream>
#include<string>
#include<map>
#include<utility>
#include<stdexcept>
#include<fstream>
#include<sstream>
using namespace std;
//测试打开文件是否成功的函数;
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
int main(int argc,char **argv)
{
map<string,string> trans_map;//trans_map存储单词转换文件
string key,value;
if(argc!=3)
throw runtime_error("wrong numbers of arguments");
ifstream map_file;
//测试文件是否打开成功
if(!open_file(map_file,argv[1]));
throw runtime_error("no transformation file");
//如果打开成功,将单词转换文件的内容存入map容器对象
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream input;
//打开需要转换的文件
if(!open_file(input,argv[2]))
throw runtime_error("no input file");
string line;
//按行读入到string对象
while(getline(input,line)){
//关联到字符串流
istringstream stream(line);
string word;
bool firstword=true;
//对一行里面的所有单词进行检测,测试是否需要转换
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword==0;
else
cout<<" ";
//输出单词
cout<<word;
}
cout<<endl;
}
return 0;
}
我的问题: 在linux下怎么采用命令行的方式将两个文件读入,我用了下./10-17 map_t.txt map.txt(10-17是我生成的可执行文件,map_t.txt是单词转换文件,map.txt是学要转换的文件)
我的执行结果:
[root@localhost 10]# ./10-17 map_t.txt map.txt
terminate called after throwing an instance of 'std::runtime_error'
what(): no transformation file
已放弃 (core dumped)
[root@localhost 10]#