在文件中查找多个单词,然后提取这些单词后面的值

Findind more than one word in a file and then extracting the values present after those words?

本文关键字:单词 提取 文件 查找 然后      更新时间:2023-10-16

注意:我问了一个类似的问题,但由于我没有提供代码(我想),这个问题被"搁置"了。现在我也写了代码,但我还面临一些其他问题。

从我的.bench文件中,我必须读取写入制动器()的值,这是我设法做到的。但问题是,我读取了INPUTOUTPUTNAND之后的制动器中的值。

.bench file

INPUT(1)
INPUT(2)
INPUT(3)
INPUT(6)
INPUT(7)
OUTPUT(22)
OUTPUT(23)
10 = NAND(1, 3)
11 = NAND(3, 6)
16 = NAND(2, 11)
19 = NAND(11, 7)
22 = NAND(10, 16)
23 = NAND(16, 19)

到目前为止,我已经编写了代码来查找INPUTOUTPUTNAND之后括号内的值,但可以看出,我正在一次又一次地重复类似的代码行那么,我如何推广相同的代码来查找OUTPUTNAND等之后的值。

int Circuit::readBenchFile(string filename) //read the benchfile and generate inputs, outputs and gates accordingly
{
    //Reading the .bench file
    ifstream input_file;
    char * S = new char[filename.length() + 1];
    std::strcpy(S,filename.c_str());
    input_file.open(S);
    if(input_file.fail())
    {
        cout << "Failed to open Bench file.n";
        return 1;
    }
    ///////
string line;
string guard_str("#");
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))  
{
    std::size_t guard_found = line.find(guard_str);
    if (guard_found ==std::string::npos)
    {
        ///Input
        std::size_t found = line.find(input_str);
        if (found!=std::string::npos)
        {
            found = line.find_first_of('(', found + 1);
            //Getting our input name and printing it.
            string out = line.substr( found + 1, ( line.find_first_of(')', found) - found - 1) );
            std::cout << out << std::endl;
        }
        ///Output
        std::size_t found1 = line.find(output_str);
        if (found1!=std::string::npos)
        {
            found1 = line.find_first_of('(', found1 + 1);
            //Getting our input name and printing it.
            string out = line.substr( found1 + 1, ( line.find_first_of(')', found1) - found1 - 1) );
            std::cout << out << std::endl;
        }       
        ///NAND
        std::size_t found_2 = line.find(nand_str);
        if (found_2!=std::string::npos)
        {
            found_2 = line.find_first_of('(', found_2 + 1);
            //find first input
            string first_input = line.substr( found_2 + 1, ( line.find_first_of(',', found_2) - found_2 - 1) );
            //Second input
            found_2 = line.find_first_of(',', found_2 + 2);
            string second_input = line.substr( found_2 + 1, ( line.find_first_of(')', found_2) - found_2 - 1) );
            cout<<"nInputs to NAND gate are:  "<<( first_input + string(" & ") + second_input );
        }           
    }
}           

}

我想最好的方法是使用正则表达式。好的选择是Boost Regex库来做到这一点:http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/index.html.

如果你不熟悉正则表达式,这里有一个很棒的页面,可以让你很快上手:http://www.regular-expressions.info/.主页上的第一段会告诉你这个想法。

简而言之:正则表达式可以快速查找文本中的模式。您可以快速构建一个正则表达式和依赖于它的函数,如果找到您要查找的任何单词,该函数将返回true。

如果您正在寻找泛型,我建议使用boost::split。

vector<string> result;
vector<string> value2;
vector<string> nand_case;
boost::split(result , myline, boost::is_any_of("("));
boost::split(value2, result[1], boost::is_any_of(")"));
if (result[0].find("NAND") != string::pos)
  boost::split(nand_case, value2[0], boost::is_any_of(",");

会给你输入(23):

result[0] : INPUT
result[1] : 23)
value2[0] : 23    

会给你输出(18):

result[0] : OUTPUT
result[1] : 18)
value2[0] : 18  

将给你23=NAND(16,19):

result[0] : 23 = NAND
result[1] : 16, 19)
value2[0] : 16, 19
nand_case[0] : 16
nand_case[1] : 19

希望我能正确理解,这能有所帮助。