| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 721 人关注过本帖
标题:求助:如何读入文件!附一段程序。。
只看楼主 加入收藏
dyc9422
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-5-19
收藏
 问题点数:0 回复次数:2 
求助:如何读入文件!附一段程序。。
#include <iostream>
#include <map>
#include <fstream>
#include <sstream>
#include <stdexcept> /*throw*/
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 (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;
}
getchar();
return 0;
}


程序编译通过。。运行一闪而过。。没看清楚是什么。。
程序大意:输入两个文件,第一个文件包含诺干单词对,每对的第一个单词出现在输入的字符串中,而第二个单词则是用于输出。。
本质上这个文件提供的是单词转换的集合——在遇到第一个单词时,应该将之替换为第二个单词。第二个文件则提供了需要转换的文本。

就是不知道怎么读入文件。。求教。。谢谢。。。
搜索更多相关主题的帖子: map include string 文件 
2007-05-27 19:36
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

把最后句getchar()改为system("pause")试试!


Fight  to win  or  die...
2007-05-27 20:17
shenjianyz
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-5-28
收藏
得分:0 
这个程序很好啊,运行没有任何问题
http://hi.baidu.com/shenjianyz
2007-05-28 15:28
快速回复:求助:如何读入文件!附一段程序。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014474 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved