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


Note: I asked a similar question, but it was placed on "hold" Because I do not provide my code (I think). Now I have written my code, but I have to face some other problem.

From my .bench file, I have to read the values ​​written in the brackets () which I managed to do but the problem is that I INPUT , OUTPUT , NAND . After the values ​​in the brakets have read.

INPUT (1) INPUT (2) INPUT (3) INPUT (6) INPUT (7) Output (22) Output ( .bench file

So far, I used the code INKUT , OUTPUT , and NAND but as it looked I can repeat the same lines of code again and again again. So, how can I normalize the same code to find the value after OUTPUT , NAND etc.

  int circuit :: readBenchFile (string filename) // Read the benchfile and generate inputs, outputs and gates accordingly {// .bench file ifstream input_file Reading; Char * S = new char [filename.length () + 1]; Std :: strcpy (s, filename.c_str ()); Input_file.open (s); If (input_file.fail ()) {cout & lt; & Lt; "Failed to open bench file. \ N"; Return 1; } /////// string line; String guard_st ("#"); String input_st ("INPUT"), output_st ("OUTPUT"), nand_st ("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); String out = line.substr (found + 1, 1 = 1, (line.fund_first_off (')', found) - found - 1)); Std :: cout & lt; & Lt; Outside & lt; & Lt; Std :: endl; } /// output std :: size_t found1 = line.find (output_str); String out = line.substr (found1 + 1, 1 has been found, 1) (line.find_first_of (')', found 1) - found 1 - 1)); Std :: cout & lt; & Lt; Outside & lt; & Lt; 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); // get 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 seconds_input = line. substr (found_2 + 1, (line.find_first_of (') ', Found_2) - found_2 - 1)); Cout & lt; & Lt; "\ nThe input to the nand gate is:" & lt; & Lt; (First_input + string ("& amp;") + seconds_input); }}}}}    

I think the best way to do this is to use regular expressions To use The best option is the Boost Reggae library, to do this.

If you are not familiar with regular expressions, then this is a very good page that will start you very fast. The first paragraph on the main page will give you this idea.

In short: Regular expressions make it possible to get patterns quickly in the text. You can quickly build a function based on a regular expression and the one which will be true if you find any word you find.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -