首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++ Primer第四版中一个程序运行时疏失,麻烦各位看看

2012-06-24 
C++ Primer第四版中一个程序运行时出错,麻烦各位看看程序干的事情就是读入一个文件,这个文件的内容时一个“

C++ Primer第四版中一个程序运行时出错,麻烦各位看看
程序干的事情就是读入一个文件,这个文件的内容时一个“替换表”,然后读入另一个文件然后用替换表中的单词替换文件中对应的单词。(程序在中文版书的318页,我把其中的open_file函数也给出了):
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stdexcept>
#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;
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(cin>>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;
}

执行时,我先把那两个文件拷贝到源文件的目录下,然后在命令行里执行:map.exe trans-map trans-text
可是程序却报错:
R6010
-abort() has been called
求解释啊!

[解决办法]
建议:单步调式。

热点排行