如何为输入文件构建解析器

发布于 2024-11-02 10:22:04 字数 269 浏览 1 评论 0原文

我可以获得一些为输入文件构建解析器的指导吗?我几周来一直在寻求帮助,作业已经过期了,我只想知道如何做。

注释的代码是我尝试过的,但我感觉它比这更严重。我有一个文本文件,我想解析它以计算单词在文档中出现的次数。

   Parser::Parser(string filename) {
   //ifstream.open(filename);

  // source (filename, fstream::in | fstream::out);

 }

can i please get some guidance to constructing a parser for an input file, I've been looking for a help for weeks, the assignment is already past due, I would just like to know how to do it.

The commented code is what I've tried, but i have a feeling it is more serious than that. I have a text file and I want to parse it to count the number of times that words appear in the document.

   Parser::Parser(string filename) {
   //ifstream.open(filename);

  // source (filename, fstream::in | fstream::out);

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-11-09 10:22:04

注释的代码是我尝试过的,但我有一种感觉,它比这更严重。

我有一种感觉,你什么都没尝试过。所以我也会这样做。

Google 是你的朋友

The commented code is what I've tried, but i have a feeling it is more serious than that.

I have a feeling you haven't tried a thing. So I am going to do the same.

Google is your friend.

苦妄 2024-11-09 10:22:04

读取单词:

std::ifstream  file("FileName");

std::string word;
file >> word; // reads one word from a file.


// Testing a word:
if (word == "Floccinaucinihilipilification")
{
     ++count;
}

// Count multiple words
std::map<std::string, int>   count;

// read a word
++count[word];

// To read many words from a file:

std::string word;
while(file >> word)
{
     // You have now read a word from a file
}

注意:这是一个真实的单词 :-)
http://dictionary.reference.com/browse/floccinaucinihilipilification

To read a word:

std::ifstream  file("FileName");

std::string word;
file >> word; // reads one word from a file.


// Testing a word:
if (word == "Floccinaucinihilipilification")
{
     ++count;
}

// Count multiple words
std::map<std::string, int>   count;

// read a word
++count[word];

// To read many words from a file:

std::string word;
while(file >> word)
{
     // You have now read a word from a file
}

Note: That is a real word :-)
http://dictionary.reference.com/browse/floccinaucinihilipilification

梦断已成空 2024-11-09 10:22:04

查看如何你用 C++ 从文件中读取一个单词吗? .最简单的方法是使用 ifstreamoperator>> 读取单个单词。然后,您可以使用诸如 vector (如上面的链接中所述)或 map 之类的标准容器来记住实际计数。

Take a look at the answers in How do you read a word in from a file in C++? . The easiest way is to use an ifstream and operator>> to read single words. You can then use a standard container like vector (as mentioned in the link above) or map<string, int> to remember the actual count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文