【问题标题】:Searching for a phrase in a text file c++在文本文件c ++中搜索短语
【发布时间】:2014-11-08 03:30:36
【问题描述】:

我正在尝试读取文本文件以查找短语/句子(/substring?)出现的次数。我目前已经完成了一项真正的工作(参见下面的代码),但正如您所看到的,它依赖于一些相当笨拙的 if 语句。

我无法访问我将在家里使用它的文件,所以我使用了一个名为 big.txt 的文件并暂时搜索“and the”之类的短语。

理想情况下,我希望能够搜索“此错误代码 1”并返回它发生的次数。任何关于如何让我的代码以这种方式工作的想法都会非常有用!

int fileSearch(string errorNameOne, string errorNameTwo, string textFile) {

string output;                                  //variable that will store word from text file


ifstream inFile;                        
inFile.open(textFile);                          //open the selected text file

if (!inFile.is_open()) {
    cerr << "The file cannot be opened";
    exit(1);
}

if (inFile.is_open()) {                         //Check to make sure the file has opened correctly

    while (!inFile.eof()) {                     //While the file is NOT at the end of the file

        inFile >> output;                       //Send the data from the file to "output" as a string

        if (output == errorNameOne) {           //Check to look for first word of error code
            marker = 1;                         //If this word is present, set a marker to 1
        }

        else if (marker == 1) {                 //If the marker is set to 1,
            if (output == errorNameTwo) {       //and if the word matches the second error code...
                count++;                        //increse count
            }

            marker = 0;                         //either way, set marker to 0 again
        }
    }
}

inFile.close();                                 //Close the opened file

return count;                                   //Function returns count of error
}

【问题讨论】:

  • 我无法理解您的问题。您能否添加一些带有一些虚拟数据的示例,例如:您的输入是什么以及预期的输出是什么。
  • 您说您正在寻找这个短语:“this error code 1”,但您的示例是一个接一个地寻找两个短语。您在寻找一两个短语吗?文本文件中的短语还有多大的可变性?它总是精确固定还是空格数可以改变(例如)。数字是固定为 1 还是可以是其他数字,例如:“此错误代码 7”?
  • @Galik 在实际的日志文件中,该短语将读取(例如)“Header Tangs Present”,然后是一个空格,然后是一个 1。它只能有一个 1 或一个 0 和所有的错误代码在看起来像表格的表格中逐行列出(但它只是一个格式化的文本文件)。我正在寻找两个短语的原因是,当字符串从 inFile 发送到输出时,它一次只做一个单词,所以它不会识别第二个单词,因此标记和检查下一个单词.
  • 那么每一行只有一个短语是真的吗?或者该短语可以在每一行出现几次?
  • @Galik 有问题的短语只会在一行中出现一次。我不能只匹配整行,因为它也以时间戳开头,每个实例都不同。该行可能是:......header tangs present 1

标签: c++ string file search text


【解决方案1】:

鉴于您的短语每行只能出现一次,并且数字跟在短语后面的多个空格之后,您可以逐行读取文件并使用 std::string::find() 来查看您的短语在某处在行中。这将返回短语的位置。然后,您可以在该短语之后立即检查该行的其余部分,以测试数字是 1 还是 0。

这段代码可能不是你想要的(仍然不确定确切的规格),但希望它应该包含足够的例子来说明你可以做些什么来实现你的目标。

// pass the open file stream in to this function along with the 
// phrase you are looking for and the number to check
int count(std::istream& is, const std::string& phrase, const int value)
{
    int count = 0;

    std::string line;
    while(std::getline(is, line)) // read the stream line by line
    {
        // check if the phrase appears somewhere in the line (pos)
        std::string::size_type pos = line.find(phrase);

        if(pos != std::string::npos) // phrase found pos = position of phrase beginning
        {
            // turn the part of the line after the phrase into an input-stream
            std::istringstream iss(line.substr(pos + phrase.size()));

            // attempt to read a number and check if the number is what we want
            int v;
            if(iss >> v && v == value)
                ++count;
        }

    }
    return count;
}

int main()
{
    const std::string file = "tmp.txt";

    std::ifstream ifs(file);

    if(!ifs.is_open())
    {
        std::cerr << "ERROR: Unable to open file: " << file << '\n';
        return -1;
    }

    std::cout << "count: " << count(ifs, "Header Tangs Present", 1) << '\n';
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多