〓〓〓〓菜鸟问题〓〓〓〓

starsoulxp 2004-08-02 02:12:20
给定一篇英文文章,统计一下里面英文单词的个数,但是我不知道怎么读文件?读文件代码应该怎么写?谢谢。
...全文
140 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
chunhai12 2004-08-03
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit()
#include <list>
#include <string>

using namespace std;

template <typename T>
void writeList(const list<T>& alist, const string& separator);

// maintains a word and its frequency of occurrence
class wordFreq
{
public:
// initialize word and set freq to 1
wordFreq(const string& str): word(str), freq(1)
{}

// add 1 to the frequency
void increment()
{ freq++; }

// equality operator compares the word for the objects
friend bool operator== (const wordFreq& lhs, const wordFreq& rhs)
{ return lhs.word == rhs.word; }

// less than operator compares the word for the objects
friend bool operator< (const wordFreq& lhs, const wordFreq& rhs)
{ return lhs.word < rhs.word; }

// output an object in the format: word (freq)
friend ostream& operator<< (ostream& ostr, const wordFreq& w)
{
ostr << w.word << " (" << w.freq << ')';
return ostr;
}

private:
string word;
// number of times word found
int freq;
};

template <typename T>
list<T>::iterator seqSearch(list<T>::iterator first,
list<T>::iterator last, const T& target);

int main()
{
ifstream fin;
// words read from file and inserted into wf
list<wordFreq> wf;
// use for seqSearch() and displaying the list
list<wordFreq>::iterator iter;
// prompt for the name of the file
string fileName, word;

cout << "Enter the name of the file containing the words: ";
cin >> fileName;

// error checking
fin.open(fileName.c_str());
if (!fin)
{
cerr << "Cannot open " << fileName << endl;
exit(1);
}

// read a word until end-of-file
while (fin >> word)
{
// declare a wordFreq object with frequency 1
wordFreq obj(word);

// search for word in the list wf
iter = seqSearch<wordFreq> (wf.begin(), wf.end(), obj);

// did we locate the word?
if (iter != wf.end())
// yes. increment the word frequency
(*iter).increment();
else
// word is new. insert obj into the list
wf.push_back(obj);
}

// list member function sort() orders the list
wf.sort();

// output each object on a separate line
cout << endl;
writeList(wf, "\n");

system("pause");
return 0;
}

template <typename T>
list<T>::iterator seqSearch(list<T>::iterator first,
list<T>::iterator last, const T& target)
{
// start at location first
list<T>::iterator iter = first;

// compare list elements with item until either
// we arrive at last or locate item
while(iter != last && !(*iter == target))
iter++;

// iter either points at item or is last
return iter;
}

template <typename T>
void writeList(const list<T>& alist, const string& separator = " ")
{
list<T>::const_iterator iter;

for (iter = alist.begin(); iter != alist.end(); iter++)
cout << *iter << separator;
cout << endl;
}
doolin 2004-08-03
  • 打赏
  • 举报
回复
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
ifstream rdfile("c\\test.txt");
if (!rdfile)
{
cout << "Open file failed!" << endl;
return -1;
}

string temp;
int counts = 0;
while (rdfile)
{
rdfile >> temp;
count++;
}

cout << "count of words : " << counts << endl;
return 0;
}

bm1408 2004-08-03
  • 打赏
  • 举报
回复
晕!

这里的问题也需要高手吗!

谭SIR的书上就有这样的例子!

自己不动手,不要指望别人帮你!
yu4233 2004-08-03
  • 打赏
  • 举报
回复
用fopen打开你要操作的文件
然后,用fread读取该文件内容,或者用fwrite向该文件进行写操作。
有关fopen,fread,fwrite的使用方法,可查msdn即可
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
晕,高手都到哪里去了?这里真冷清
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
请高手近来看看啊,不要掉下去啊
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
//cindy0000()
请问你这段代码应该写在什么地方,写在我的那个包含main函数的cpp里吗?
cindy0000 2004-08-02
  • 打赏
  • 举报
回复
CFile file;
CStdioFile stdiofile;
CString str;
if(stdiofile.Open("myfile.txt",CFile::modeReadWrite|CFile::typeText)==1)
{
stdiofile.ReadString(str);
}
stdiofile.Close();

starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
怎么没人回啊?急啊!
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
////bohut(伯虎)
小弟初学vc,好无头绪,还望不吝赐教,帮小弟把这个完整的程序写一下,主要是读文件那一块,具体写在什么地方啊,怎么配合统计单词那部分代码?谢谢了!
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
//bohut(伯虎)
可不可以使用getchar一个一个的读
bohut 2004-08-02
  • 打赏
  • 举报
回复
FILE *fp;
fp=fopen("c:\\myfile.txt","r");
if(fp==NULL)
{
 AfxMessageBox("找不到文件!");
return -1;
}
char szBuf[MAX_SIZE];//MAX_SIZE你自己定
memset(szBuf,0,MAX_SIZE);
fread(szBuf,1,MAX_SIZE,fp);
fclose(fp);
自此,文件被读到szBuf中
sohou 2004-08-02
  • 打赏
  • 举报
回复
天啦,老兄,你你,我无语,还是老实的看看书吧,不要在这问了
starsoulxp 2004-08-02
  • 打赏
  • 举报
回复
我用#include "myfile.txt"为什么不行呢?应该怎么写》

64,685

社区成员

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

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