String operation while reading line by line from a file.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: String operation while reading line by line from a file.

  1. #1
    Join Date
    Apr 2014
    Posts
    6

    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;
    
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    Re: String operation while reading line by line from a file.

    To get the various parameters for NAND, can I refer you to my post #9 of http://forums.codeguru.com/showthrea...t=#post2153281
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,848

    Re: String operation while reading line by line from a file.

    Try this
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    ifstream input_file("c17.bench");
    	
    	if (!input_file.is_open())
    	{
    		cout << "Failed to open Bench file.\n";
    		return 1;
    	}
    
    string nline;
    
    const string nand_str = "NAND(";
    	
    	while (getline(input_file, nline))  
    	{
    		size_t	eql;
    
    		if ((eql = nline.find("=")) != string::npos)
    		{
    			size_t st = nline.find_first_not_of(" ", 0);
    			cout << "Gate " << nline.substr(st, nline.find_last_not_of(" ", eql - 1) + 1 - st) << endl;
    			
    			size_t	nand,
    				del;
    
    			if ((nand = nline.find(nand_str)) != string::npos)
    				nand += nand_str.length();
    
    			for (nand = nline.find_first_not_of(" ", nand); nand != string::npos; nand = nline.find_first_not_of(" ", del))
    				if ((del = nline.find_first_of(",)", nand)) != string::npos)
    					cout << nline.substr(nand, del++ - nand) << endl;
    
    			cout << endl;
    		}
    	}
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: String operation while reading line by line from a file.

    This is actually a pretty "simple" task for regular expressions.
    It's not the speediest solution (but neither is the solution in #3) possible, but unless you need this to blitz through thousands of lines of code, this will be fast enough. (for speed, you'll need a proper token parser that doesn't use strings as intermediaries)

    Simply make a regex for each of the possible input lines, test it, and if it matches, handle each case. Extract the parameters from the regex result object.
    the good part is that this has a built in "syntax validity check" (such as 'is a numeric parameter actually numeric') which would require additional code in the #3 approach.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center