c++ 单词转换程序

tektekman 2011-04-06 05:38:45
单词转换文件内容: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]#
...全文
246 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tektekman 2011-04-09
  • 打赏
  • 举报
回复
我个人感觉是linux下gcc/gdb比较爽。
Ping_QC 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chengliangqq 的回复:]

感谢各位朋友指点,刚才用gdb调试已经找到错误了,哎,都怪自己太粗心,一点小错误找了那么久
gdb信息部分显示:
23 if(!open_file(map_file,argv[1]));
(gdb) n
24 throw runtime_error("no transformation file");
(gdb) n
terminate called a……
[/Quote]


linux 下编程很有诱惑啊。但对于命令行就有点恐惧了
tektekman 2011-04-06
  • 打赏
  • 举报
回复
感谢各位朋友指点,刚才用gdb调试已经找到错误了,哎,都怪自己太粗心,一点小错误找了那么久
gdb信息部分显示:
23 if(!open_file(map_file,argv[1]));
(gdb) n
24 throw runtime_error("no transformation file");
(gdb) n
terminate called after throwing an instance of 'std::runtime_error'
what(): no transformation file

Program received signal SIGABRT, Aborted.

原来是if后面多了一个“ ; ”;现在已经ok了,对了有没有对linux学习的爱好者,加个好友共同学习一下;
qiangv 2011-04-06
  • 打赏
  • 举报
回复
int main(int argc,char **argv)
argc就是参数个数, argv是参数数组
你的问题不是问题,程序里已经解决了。

你的问题是core dumped。调试程序吧。
pathuang68 2011-04-06
  • 打赏
  • 举报
回复
思路很清楚,估计那个地方有glitches
pengzhixi 2011-04-06
  • 打赏
  • 举报
回复
不是提示no transformation file

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧