标题:关于fstream 的问题
只看楼主
wdtk
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-10-10
 问题点数:0 回复次数:2 
关于fstream 的问题

//把要排除的单词 存如vector中, 然后输出单词出现的次数.



#include<iostream>
#include<set>
#include<map>
#include<string>
#include<vector>
#include<fstream>
using namespace std;

void restricted_wc(ifstream &removeFile,map<string,int> &wordCount)
{
vector<string>excluded;
string removeWord;
while(removeFile>>removeWord)
{
excluded.push_back(removeWord);
}
cout<<"enter text:"<<endl;
string word;
while(cin>>word)
{
bool find=false;
vector<string>::iterator iter=excluded.begin();
while(iter!=excluded.end())
{
if(*iter==word)
{
find=true;
break;
}
++iter;
}
if(!find)
++wordCount[word];
}
}

int main()
{
map<string,int> wordCount;
string fileName;
cout<<"enter filename:"<<endl;
cin>>fileName;
ifstream exFile(fileName.c_str());
if(!exFile) //代码正确,程序在打开文件时,总是提示"文件不能打开".是不是文件没放到正确位置啊~~~.请教~~~!
{
cout<<"error: can not open file:"<<fileName<<endl;
return -1;
}
restricted_wc(exFile,wordCount);
cout<<"word\t\t"<<"time"<<endl;
map<string,int>::iterator iter=wordCount.begin();
while(iter!=wordCount.end())
{
cout<<iter->first<<" "<<iter->second<<endl;
++iter;
}
return 0;
}

[此贴子已经被作者于2007-10-21 11:18:31编辑过]

搜索更多相关主题的帖子: fstream 
2007-10-21 11:15
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
得分:0 
把文件放到和程序一起.
然后如果是1.txt就直接输入1.txt
文件类型得有
2007-10-21 11:47
wdtk
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2007-10-10
得分:0 
回复:(天使梦魔)把文件放到和程序一起.然后如果是1...
恩,是了,没加后缀名.谢了
2007-10-21 11:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-179479-1-1.html




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

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