|
-
May 12th, 2014, 08:55 AM
#1
String operation while reading line by line from a file.
I have to read the information about logic gates from a file. The file format is shown below:
Code:
gateOne = NAND(inpA, inpB)
gate2=NAND(1,2)
3 = NAND(23,25,26)
As, it can be seen from the above structure that whitespaces are not same everytime. So, to deal with this situation, i am using boost library to remove all whitespaces from the line which is being read and then try to find the name of gate and its input. My code is given below which is able to correctly find the gate names and its first input...but my code is not able to find the second, third and so on input names.
Code:
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <boost/algorithm/string/erase.hpp>
using namespace std;
int main(int argc, char* argv[])
{
//Reading the .bench file
ifstream input_file;
input_file.open("c17.bench");
if(input_file.fail())
{
cout << "Failed to open Bench file.\n";
return 1;
}
///////
string line;
while (getline( input_file, line ))
{
boost::algorithm::erase_all(line, " ");
cout<<"\n"<<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 - 1);
cout<<"\nGate:"<<gate_name<<" have input: ";
first_index_nand += nand_str.length() - 1;
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<<"\n"<<input_name;
}
}
}
}
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|