I am trying to read the logic gates names and their inputs from a file. I have been given a .bench
file which gives the information about the gate name and its inputs.
I have written a code below which gives me perfect results if the information is given in the following format:
firstGate = NAND(inpA, inpB, inpC)
secGate = NAND(1, 2)
30 = NAND(A, B)
PROBLEM: But if there is a change in the "white space" before =
sign , after ,
or at some other place then my code doesn't work. For
example, if the file is given in the following format then i am not able to read it correctly
first=NAND(inpA, inpB, inpC) //no space before and after "="
sec = NAND(1,2) //no space after ","
My code which is working for the first case is below:
int main(int argc, char* argv[])
{
//Reading the .bench file
ifstream input_file;
input_file.open("circuit.bench");
if(input_file.fail())
{
cout << "Failed to open Bench file.\n";
return 1;
}
///////
string line;
while (getline( input_file, line ))
{
///For NAND
size_t first_index_nand, second_index_nand;
string gate_name;
const string nand_str = "NAND(";
if ((first_index_nand = line.find(nand_str)) != string::npos)
{
gate_name = line.substr(0, first_index_nand - 3);
cout<<"\nGate name: "<<gate_name;
first_index_nand += nand_str.length() - 1;
cout<<"\nInput to this gate: ";
for (; first_index_nand != string::npos; first_index_nand = second_index_nand)
{
if ((second_index_nand = line.find_first_of(",)", first_index_nand)) != string::npos)
{
string input_name = line.substr(first_index_nand + 1, second_index_nand++ - first_index_nand - 1);
cout<<" "<<input_name;
}
}
}
cout<<"\n";
}
return 0;
}
Query: How should i modify my code in such a way that it should be able to read the name of gate and its inputs irrespective of their position w.r.t whitespaces?
Note: I have to deal with this problem using C++ code and its libraries only.