在文件中查找多个单词,然后提取这些单词后面的值
Findind more than one word in a file and then extracting the values present after those words?
注意:我问了一个类似的问题,但由于我没有提供代码(我想),这个问题被"搁置"了。现在我也写了代码,但我还面临一些其他问题。
从我的.bench
文件中,我必须读取写入制动器()
的值,这是我设法做到的。但问题是,我读取了INPUT
、OUTPUT
、NAND
之后的制动器中的值。
.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)
到目前为止,我已经编写了代码来查找INPUT
、OUTPUT
和NAND
之后括号内的值,但可以看出,我正在一次又一次地重复类似的代码行那么,我如何推广相同的代码来查找OUTPUT
、NAND
等之后的值。
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
希望我能正确理解,这能有所帮助。
相关文章:
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- 提取字符串中的随机单词
- 当字符串中只有一个"GOOD"字符串可用时,从没有空格的字符串中提取第一个单词的最佳和有效方法?
- 如何在C 中不带有弦乐和strtok的情况下拆分字符串(提取单词)
- 从微妙的问题中提取单词
- 提取第二个单词.解析字符串(C )
- 从长时间中提取每个单词的钻头操作
- 从C中的文件中提取单词
- 在单词词典中提取以片段开头/包含/结束的单词
- RegexC++:从字符串中提取子字符串,然后计算每个单词
- 提取两个单词之间的域
- C++编写一个从段落中提取单词的函数
- 从以特定单词开头的目录中提取所有文件
- 无法从单词中提取啃咬
- 这是从连续的单词序列中提取任意范围的位的最有效方法
- 从文件中提取行.txt然后将单词存储到单独的数组中 |C++
- 从未知长度的手动输入中提取单个单词
- c ++ 如何提取单词之间的空格(如果有)
- 提取c++输出中某个单词前面的值
- 从包含单词的文件中提取double