PARSING THE TEXT FILE...

hi i am srinivas..
i got stuck with the following code while parsing my below text file.could you please give me the errors what i did in the code.the following code is description of the circuit.

MY TEXT FILE:

INPUT(1)
INPUT(2)
INPUT(3)
INPUT(6)
INPUT(7)

OUTPUT(223)
OUTPUT(329)
OUTPUT(370)
OUTPUT(421)
OUTPUT(430)
OUTPUT(431)
OUTPUT(432)

10 = NAND(1, 3)
11 = NAND(3, 6)
19 = NAND(11, 7)
118 = NOT(1)
119 = NOT(4)
122 = NOT(11)
22 = NAND(10, 16)
23 = NAND(16, 19)
123 = NOT(17)
416 = AND(381, 386, 393, 399, 404, 407, 411, 414)
130 = NOT(37)
16 = NAND(2, 11)
183 = NOR(21, 123)
184 = NOR(27, 123)
185 = NOR(34, 127)
186 = NOR(40, 127)
187 = NOR(47, 131)
224 = XOR(203, 154)
227 = XOR(203, 159)
230 = XOR(203, 162)
404 = NAND(256, 344, 376, 69)
407 = NAND(257, 345, 377, 82)
411 = NAND(258, 346, 378, 95)
414 = NAND(259, 347, 379, 108)
233 = XOR(203, 165)

From the above text file i want to extract the design information of each gate i.e what are the inputs going into particular gate and the corresponding output of that gate.
L.H.S represents the output of the gate and R.H.S (with in the brackets) represents the inputs of the gate.
at the same time i want to display the INPUTS & OUTPUTS in the design.

MY CODE IS LIKE THIS:

#include <fstream>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

void parse(ifstream & stream, vector<int> & vec)
{
char ch;
int flt;

while (stream.get(ch))
{
if (ch == '(')
{
//cout<<"stream display>>>>"<<stream.get(flt)<<endl;
stream>> flt;
vec.push_back(flt);
//cout<<"####"<<flt;
}

else if (ch == ',')
{
stream >> flt;
vec.push_back(flt);
}

else if ( ch == ')')
{
stream.ignore(INT_MAX,'\n');
}
if ( ch == 'O') //|| ch == 'I' )//|| ch == 'X' || ch == ' ')
{
return;
}

}
}
int main (int argc, const char * argv[])
{

signed int i;
ifstream stream;
string my_file = "c17.bench";
string data_set;
vector<int> vObject;

// Open stream.
stream.open(my_file.c_str());

while (!stream.fail())
{
// Search for next data_set
stream >> data_set;

// Start looking for Objects.

if (data_set.find( "INPUT" ) != string::npos)
{
parse(stream,vObject);
}

/* Start looking for Textures.
if (data_set.find( "INPUT" ) != string::npos)
{
parse(stream,vObject);
}*/

}

// Print it out.
cout << "Extracted Data is >>>>" << endl;
for (i = 0; i <= vObject.size();++i)
{
cout << vObject[i] << endl;//"\t" << vObject[i + 1] << endl;

}

/*cout << "\n another design Data" << endl;
for (i = 0; i < vObject.size();)
{
cout << vObject[i] << endl;
i += 1;
}*/


return EXIT_SUCCESS;
}

For the above code i got the output but its wrong.
The above code im using for displaying the INPUTS of design.
but its giving the output like this
2
3
6
7
it is skipping the input 1.

please try to help me in this code.

Hi folks...
I coded my concept like this...

#include <fstream>
#include <iostream>
#include <limits>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;


/*void parse(string dataset)
{

cout<<"New"<<dataset<<endl;
for(string::size_type i=0;i!=dataset.size();++i){
if(isdigit(dataset[i])){
cout<<dataset[i]<<endl;
vObject.push_back(dataset[i]);
}
}

}*/

int main (int argc, const char * argv[])
{
unsigned int i;
ifstream stream;
string my_file = "c17.bench";
string data_set;
vector<float> vObject;

// Open stream.

stream.open(my_file.c_str());
cout<<"stream is>>>>>"<<stream<<endl;
while (!stream.fail())
{

while(stream>>data_set){
// Start looking for Objects.
if (data_set.find( "INPUT" ) != string::npos)
{
/*cout<<data_set<<endl;
parse(data_set);*/
for(string::size_type i=0;i!=data_set.size();++i){
if(isdigit(data_set[i])){
cout<<data_set[i]<<endl;

vObject.push_back(data_set[i]);

}
}
}
}


}
// Print it out.
cout << "Extracted Data is >>>>" << endl;



for (int i = vObject.size(); i>=0;)
{
cout << vObject[i] << endl;//"\t" << vObject[i + 1] << endl;
i -= 1;
}

/*cout << "\n another design Data" << endl;
for (i = 0; i < vObject.size();)
{
cout << vObject[i] << "\n" << vObject[i + 1] << endl;
i += 2;
}*/

// return EXIT_SUCCESS;
}

but the above program generates false output for the following text file given below.....like
49
50
51
52
53

<$$$$$$$$$$$$$$$$$$$$$INPUT FILE$$$$$$$$$$$$$$$$>
# c17
# 5 inputs
# 2 outputs
# 0 inverter
# 6 gates ( 6 NANDs )

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, 1)

could you please tell me where i did the mistake?
Last edited on
I suspect the problem is with
1
2
// Search for next data_set
stream >> data_set;

data_set is a string, so will then contain
INPUT(1)
so when you then call parse you are already past the first line.

Please use the # button to format the code - it makes it much easier to read:-)
I would have two functions - one function to sort out the INPUT / OUTPUT as these linea are similar and one to scan for the LOGIC bit (the lines with the = sign).

My idea would be to scan the file line by line and for each line I would call the Input/Ouput function - if this function succeeds then get the next line if this function fails then try the logic function (while should succeed or you have a faulty formatted line) - get next line.......

Ill try it and see what I end up with.

Topic archived. No new replies allowed.