我们如何在文本文件中的C ++中找到几个单词?

时间:2018-10-05 19:25:16

标签: c++

在我的项目中,我需要整理file.txt文件中包含不同单词的列表。我需要我的程序来识别这3个名称:bob,alicia和cookie。而且,每当他发现“ cookie”为例时,我都想显示“狗”,“ alicia”,“ girl”,“ bob”,“ boy”以及其他单词“ unknown”。

文本文件包含:

hello
shirley
cookie
bob
alicia
cook
road
alicia
stole
bob

所以我做了这种类型的代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{

  ifstream input;
  size_t pos;
  string line;

  input.open("file.txt");
  if(input.is_open())
    {
      while(getline(input,line))
    {
      pos = line.find("cookie");
      pos = line.find("bob");
      pos = line.find("alicia");
      if(pos!=string::npos) // string::npos is returned if string is not found
        {
          cout <<"dog \n";
          cout <<"girl \n";
          cout <<"boy \n";

          break;
        }
    }
    }
}

并且当此代码将file.txt用作条目时,我没有这样的正确结果:

unknown
unknown
dog
boy
girl
unknown
unknown
girl
unknown
boy

因为我不知道我能得到这个结果,您能帮我吗?

2 个答案:

答案 0 :(得分:1)

这里很少有问题

  1. 您要在while循环中重写pos
  2. 如果在文件中发现dog girl and boy,则一次只能打印aliciapos覆盖问题)。
  3. 您不处理unknown的案件。

因此替换您的

while(getline(input,line))
{
  pos = line.find("cookie");
  pos = line.find("bob");
  pos = line.find("alicia");
  if(pos!=string::npos) // string::npos is returned if string is not found
    {
      cout <<"dog \n";
      cout <<"girl \n";
      cout <<"boy \n";

      break;
    }
}

while(getline(input,line))
{
    if ((line.find("bob")) != string::npos)
    {
        cout <<"boy \n";
    }
    else if ((line.find("alicia")) != string::npos)
    {
        cout <<"girl \n";
    }
    else if ((line.find("cookie")) != string::npos)
    {
        cout <<"dog\n";
    }
    else
    {
        cout <<"unknown \n";
    }
}

输出:

unknown
unknown
dog
boy
girl
unknown
unknown
girl
unknown
boy
  

注意::这种方法会在每一行中找到一个单词(包括子字符串)。

答案 1 :(得分:0)

使用流引数运算符>>会更容易:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    char const *filename{ "file.txt" };
    std::ifstream is{ filename };

    if (!is.is_open()) {
        std::cerr << "Couldn't open \"" << filename << "\" for reading!\n\n";
        return EXIT_FAILURE;
    }

    std::string word;
    while (is >> word) {
        if (word == "cookie")
            std::cout << "dog\n";
        else if (word == "bob")
            std::cout << "boy\n";
        else if (word == "alicia")
            std::cout << "girl\n";
        else
            std::cout << "unknown\n";
    }
}

如果仅要求将行的第一个单词视为一个名称,则可以编写一个操纵器,吃掉所有东西,直到遇到换行符或EOF

std::istream& eat_till_newline(std::istream& is)
{
    int ch;
    while ((ch = is.get()) != EOF && ch != '\n');
    return is;
}

用法:

while (is >> word >> eat_till_newline)

要进一步缩小单词的含义,可以从字符串的右侧删除所有非字母:

#include <string>
#include <cctype>

// ...

std::string r_trim(std::string str, int(*pred)(int))
{
    std::size_t i = str.length();

    if (!i)
        return str;

    for(; i > 0 && !pred(static_cast<int>(str[i-1])); --i);

    return str.substr(0, i);
}

用法:

while (is >> word >> eat_till_newline) {
    word = trim_r(word, std::isalpha);
    if (word == "cookie")
        std::cout << "dog\n";
    else if (word == "bob")
        std::cout << "boy\n";
    else if (word == "alicia")
        std::cout << "girl\n";
    else
        std::cout << "unknown\n";
}

将它们放在一起:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

std::istream& eat_till_newline(std::istream& is)
{
    int ch;
    while ((ch = is.get()) != EOF && ch != '\n');
    return is;
}

std::string r_trim(std::string str, int(*pred)(int))
{
    std::size_t i = str.length();

    if (!i)
        return str;

    for(; i > 0 && !std::isalpha(static_cast<int>(str[i-1])); --i);

    return str.substr(0, i);
}

int main()
{
    char const *filename{ "test.txt" };
    std::ifstream is{ filename };

    if (!is.is_open()) {
        std::cerr << "Couldn't open \"" << filename << "\" for reading!\n\n";
        return EXIT_FAILURE;
    }

    std::string word;
    while (is >> word >> eat_till_newline) {
        word = r_trim(word, std::isalpha);
        if (word == "cookie")
            std::cout << "dog\n";
        else if (word == "bob")
            std::cout << "boy\n";
        else if (word == "alicia")
            std::cout << "girl\n";
        else
            std::cout << "unknown\n";
    }
}