关于open_file函数

skineffect 2008-04-22 02:19:07
C++Primer4 Associative Container章The map type节中有道单词转换的例题如下,对open_file这个函数感觉很奇怪,似乎根本就没定义过。
#include<sstream>
#include<fstream>
#include<string>
#include<stdexcept>
#include<map>
#include<iostream>
using namespace std;
int main(int argc, char **argv)
{
// map to hold the word transformation pairs:
// key is the word to look for in the input; value is word to use in the output
map<string, string> trans_map;
string key, value;
if (argc != 3)
throw runtime_error("wrong number of arguments");
// open transformation file and check that open succeeded
ifstream map_file;
if (!open_file(map_file, argv[1])) //"open_file"undeclared,what's the meaning of the function?
throw runtime_error("no transformation file");
// read the transformation map and build the map
while (map_file >> key >> value)
trans_map.insert(make_pair(key, value));
// ok, now we're ready to do the transformations
// open the input file and check that the open succeeded
ifstream input;
if (!open_file(input, argv[2]))
throw runtime_error("no input file");
string line; // hold each line from the input
// read the text to transform it a line at a time
while (getline(input, line)) {
istringstream stream(line); // read the line a word at a time
string word;
bool firstword = true; // controls whether a space is printed
while (stream >> word) {
// ok: the actual mapwork, this part is the heart of the program
map<string, string>::const_iterator map_it =
trans_map.find(word);
// if this word is in the transformation map
if (map_it != trans_map.end())
// replace it by the transformation value in the map
word = map_it->second;
if (firstword)
firstword = false;
else
cout << " "; // print space between words
cout << word;
}
cout << endl; // done with this line of input
}
return 0;
}
自己改成这样的能运行:
#include<sstream>
#include<fstream>
#include<string>
#include<stdexcept>
#include<map>
#include<iostream>
using namespace std;
int main()
{
map<string,string> trans_map;
string key,value;
ifstream tran("map_file.txt");
if(!tran)
{
throw runtime_error("no transformation file");
}
while(tran>>key>>value)
trans_map.insert(make_pair(key,value));
ifstream in("input.txt");
if(!in)
throw runtime_error("no input file");
string line;
while(getline(in,line))
{
istringstream stream(line);
string word;
bool firstword=true; //controls whether a space is printed
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
//if this word is in the transformation map
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword=false;
else
cout<<" ";
cout<<word;
}
cout<<endl;
}
}
那位大牛给我讲解一下,谢谢了:-)


...全文
576 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
haofang666777 2011-12-07
  • 打赏
  • 举报
回复
++为这个吃了很多苦[Quote=引用 7 楼 xjy1204 的回复:]

c++ primer 有个特点,就是他的例子都是连贯的,
类也好,函数也好,整本书的过程可以看做是一步一步的优化和增强以前的类或者函数
[/Quote]
畿米阳光 2011-07-06
  • 打赏
  • 举报
回复
/// 打开文件流
/// @param in 输入流
/// @param file 文件名
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
skineffect 2008-04-24
  • 打赏
  • 举报
回复
不好意思,这本书没连着看。。。。谢了。
xjy1204 2008-04-23
  • 打赏
  • 举报
回复
c++ primer 有个特点,就是他的例子都是连贯的,
类也好,函数也好,整本书的过程可以看做是一步一步的优化和增强以前的类或者函数
taodm 2008-04-23
  • 打赏
  • 举报
回复
典型的前看后忘型读书法。
tianjiao85 2008-04-23
  • 打赏
  • 举报
回复
//"open_file"undeclared,what's the meaning of the function?
未定义标识符,
ryfdizuo 2008-04-22
  • 打赏
  • 举报
回复
8.4.3. A Program to Open and Check Input Files
这里,找本电子版的。索引搜一下就行了,
ryfdizuo 2008-04-22
  • 打赏
  • 举报
回复
    // opens in binding it to the given file
ifstream& open_file(ifstream &in, const string &file)
{
in.close(); // close in case it was already open
in.clear(); // clear any existing errors
// if the open fails, the stream will be in an invalid state
in.open(file.c_str()); // open the file we were given
return in; // condition state is good if open succeeded
}
Inhibitory 2008-04-22
  • 打赏
  • 举报
回复
在这一节的前面记得定义过这个函数, 是为了以后方便使用而定义的.
  • 打赏
  • 举报
回复
open_file是自定义的函数,你搜索一下吧,

64,651

社区成员

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

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