首页 > 解决方案 > 使用 c++98 linux 从文本文件中提取数据

问题描述

我正在尝试从纯文本文件中提取一些行。其中包含 shell 脚本可执行文件的列表以及该特定 sh 文件的一些通用键。需要从该文件中提取的所需数据应排除 .sh 文件名和 MH_TEST 键。

例如:如果我的文件 abc.lst 包含

     Cable_pull1.sh
       MH_TEST             PAR
       DUAL_DOMAIN         yes
       CAMARO_STORAGE      YES
    
     Flagship.sh
        MH_TEST            NOR
        10_Flags           yes


      Domain_Distibute.sh
        MH_TEST            NOR
        fast_path          YES
        heavy_IO           YES

如果传递的文件名为“ Cable_pull1.sh ” ,则需要从上述文件abc.lst文件中提取的请求数据如下

   DUAL_DOMAIN         yes
   CAMARO_STORAGE      YES 

如果文件名通过“ Flagship.sh ”,预期输出如下,

   10_Flags           yes

下面是我试图获得结果的代码,我有点迷失在提取所需信息的地方,请帮助我提取我正在寻找的信息

    #include<iostream>
    #include<string>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    bool findWord(string Filename, string const& find)
    {
            cout<<"Filename:"<<Filename<<"\t"<<"find:"<<find<<endl;
            ifstream iFile(Filename.c_str());
            if(!iFile)
            {
                    cerr<<"File not opened!\n";
                    return false;
            }
    
            char c;
            string content;
            while(iFile.get(c) )
            {
                    if(c != '\n')
                    {
                            content += c;
                    }
                    else
                    {
                       content = ""; //reset string after flag ',' was found
                    }
    
                    if(content == find)
                            return true;
            }
            cout<<"content:"<<content<<endl;
            return false;
    }
    
    int main()
    {
       if(!findWord("abc.lst","Cable_pull1.sh"))
          cout<<"failed"<<endl;
        else
         cout<<"success"<<endl;
       return 0;
    }
                                  

标签: linuxtexttext-extractiondata-extractionc++98

解决方案


我会利用std::getline在文件中逐行读取到std::string. 然后,您可以使用strings 成员函数find来查找您要查找的内容。

Cable_pull1.sh例如,当您找到时,您循环,再次使用std::getline,并打印后面的行,直到找到一个空行。

例子:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

bool findWord(std::string Filename, std::string const& find) {
    std::cout << "Filename:" << Filename << "\tfind:" << find << '\n';
    std::ifstream iFile(Filename.c_str());
    if(!iFile) {
        std::cerr << "File not opened!\n";
        return false;
    }

    std::string line;

    while(std::getline(iFile, line)) {     // read a whole line
        // and find "find" in that line
        std::size_t pos = line.find(find);

        // if "find" is found (pos != std::string::npos),
        // check that it's a full match to the rest of the line
        if(pos != std::string::npos && line.substr(pos) == find) {

            // ok, we found the correct entry in the file
            // loop and print each line (except the "MH_TEST" lines)

            while(std::getline(iFile, line)) {
                if(line.size()==0) break;           // empty line, break out
                if(line.find("MH_TEST") == std::string::npos) {
                    // print lines not matching MH_TEST
                    std::cout << line << '\n';
                }
            }
            return true;
        }
    }
    return false;
}

int main() {
    if(!findWord("abc.lst", "Cable_pull1.sh"))
        std::cout << "failed\n";
    else
        std::cout << "success\n";
    return 0;
}

输出:

Filename:abc.lst        find:Cable_pull1.sh
       DUAL_DOMAIN         yes
       CAMARO_STORAGE      YES
success

推荐阅读