求救c++primer4中的单词转换程序

leijian_111 2009-04-24 02:13:07
下列编译通过,执行的时候出错,求教高手解决问题(test_in.txt和test_out.txt这两个文件在哪里建立?)
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <fstream>
#include <sstream>

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 argumengs");
ifstream map_file;
if(!open_file(map_file,"test_in.txt"))
throw runtime_error("no transformation file");
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));

ifstream input;
if(!open_file(input,"test_out.txt"))
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.clear();
in.open(file.c_str());
return in;
}
...全文
164 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sou2012 2009-04-24
  • 打赏
  • 举报
回复
去作者的网站上有下载
lingyin55 2009-04-24
  • 打赏
  • 举报
回复
看下这个吧
运行时,步骤如下

开始--运行--cmd
进入exe所在目录,其中hehe.txt haha.txt放在exe的所在目录下。
输入下面命令行运行即可
test hehe.txt haha.txt

其中hehe.txt中内容是:
'em them
cuz because
gratz grateful
i I
nah no
pos supposed
sez said
tanx thanks
wuz was
haha.txt的内容是:
nah i sez tanx cuz i wuz pos to
not cuz i wuz gratz
//程序的主要功能是读取hehe中的单词转换文本并保存到map容器中,然后以此为依据转换haha中的句子并输出。

#include <iostream>
#include <string>
#include <utility>

#include <map>
#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;
string key, value;
if (argc != 3)
throw runtime_error("wrong number of arguments");//如果命令数不等于3,抛出异常
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;
}
我们可以任意修改haha中的内容,这样输出内容也会由之变化,按照我机器上的haha.txt中的内容,你不妨对照一下,很显然将输出:no I said thanks because I was supposed to not because I was grateful.

http://blog.sina.com.cn/s/blog_49652a2d0100bbrn.html
lingyin55 2009-04-24
  • 打赏
  • 举报
回复

#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <fstream>
#include <sstream>

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 argumengs"); //你抛出异常后要用catch对异常进行捕获和处理啊,不然程序
//会调用默认的abort()函数,使得程序被终止。
ifstream map_file;
if(!open_file(map_file,"test_in.txt"));
//throw runtime_error("no transformation file");
while(map_file>>key>>value)
trans_map.insert(make_pair(key,value));

ifstream input;
if(!open_file(input,"test_out.txt")) ;
//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.clear();
in.open(file.c_str());
return in;
}
leijian_111 2009-04-24
  • 打赏
  • 举报
回复
我在Debug目录下和当前工程目录下,都试过还是出错,
sytstarac 2009-04-24
  • 打赏
  • 举报
回复
要在你的这个程序源文件所在的目录里有test_In和test_out这两个文件才可以。
bfhtian 2009-04-24
  • 打赏
  • 举报
回复
和debug在同一路径下的同名文件内
chenzhp 2009-04-24
  • 打赏
  • 举报
回复
如果用VC6的话就在当前工程的debug目录下建立。
w0911h 2009-04-24
  • 打赏
  • 举报
回复
没仔细看代码,不过这两个文件应该在当前程序所在目录下建立

65,211

社区成员

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

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