c++单词转换程序

Tinide 2012-11-28 10:27:16
c++primer 第十二章的单词转换程序运行后出现debug




c++primer 第十二章的单词转换程序运行后出现debug






// 单词转换.cpp
#include<iostream>
#include<map>
#include<fstream>
#include<sstream>
#include<string>

using namespace std;

ifstream& open_file(ifstream& in,const string&file);



int main(int argc ,char **argv)
{
map<string,string> trans_map;
string key,value;
if(argc!=3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if(!open_file(map_file,argv[1]))
throw runtime_error("no transformation file");
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;
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=false;
else
cout<<" ";
cout<<word;

}
cout<<endl;

}
return 0;
}

ifstream& open_file(ifstream& in,const string&file)
{
in.close();
in.open(file.c_str());
return in;

}
...全文
123 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tinide 2012-11-29
  • 打赏
  • 举报
回复
可是还是不能运行有错啊 怎么回事 谢谢


doki0314 2012-11-29
  • 打赏
  • 举报
回复
换Release模式编译试试?
赵4老师 2012-11-29
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。
somksomk 2012-11-29
  • 打赏
  • 举报
回复
ifstream& open_file(ifstream& in,const string&file) { //in.close(); in.open(file.c_str()); return in; } 文件都没打开。你不能close
somksomk 2012-11-29
  • 打赏
  • 举报
回复
文件打开不成功
hznat 2012-11-29
  • 打赏
  • 举报
回复
你是直接运行的? 这个程序需要生成可执行文件,然后使用命令行带参数执行。如果生成的可执行文件为test.exe,那可以在CMD窗口中执行: test.ext a b 还有一点当参数不等于3个时,会继续往下执行,报你现在的错误。 所以:
if(argc!=3)
{
     hrow runtime_error("wrong number of arguments");
     return 0;   // 后面没有带2个参数,应该退出
}

64,648

社区成员

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

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