Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write rest of line to variable 2

Status
Not open for further replies.

NetworkGhost

IS-IT--Management
Apr 12, 2005
1,324
US
I have an issue where I am outputting a line into some string variables. What I would like to due is place the remainder of the line into a one variable

The line looks like this:

access-list 100 permit ip any host 122.22.22.22

getline(configData, line) >> acl >> aclname >> aclaction >> remainder;


I know the above is wrong but I would like the value of remainder to be "ip any host 122.22.22.22"


Not sure how to get it done. Help is appreciated.

 
Just use getline after you've read in everything else. Doesn't that work?

Also, if you're calling getline first, doesn't that read in the entire line, messing up your other variables?
 
It only reads the first 3 words.

acl = access-list

aclname = 100

aclaction = permit

I want "ip any host 122.22.22.22" to go to a variable also. If I use getline again it seems like it just pull from the next line.


 
Works for me. Code:
Code:
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    string acl, aclname, aclaction, remainder;
    cin >> acl >> aclname >> aclaction;
    getline(cin, remainder);
    cout << "acl=" << acl << '\n';
    cout << "aclname=" << aclname << '\n';
    cout << "aclaction=" << aclaction << '\n';
    cout << "remainder=" << remainder << '\n';
}
Input:
Code:
access-list 100 permit ip any host 122.22.22.22
Output:
Code:
acl=access-list
aclname=100
aclaction=permit
remainder= ip any host 122.22.22.22
 
Did you try this:
Code:
configData >> acl >> aclname >> aclaction;
getline(configData, remainder);
 
Here is the file I am pulling from:

fixup protocol smtp 25
fixup protocol sqlnet 1521
fixup protocol tftp 69
names
object-group network GRP_INT_NETWORKS
network-object host 10.1.1.1
network-object host 192.168.0.0
object-group network test-group
network-object host 10.10.10.10
network-object 192.168.1.0 255.255.255.0
network-object 192.168.50.0 255.255.255.0
network-object 192.168.60.0 255.255.255.0
access-list acl_inside permit ip any any
access-list acl_inside permit ip any any
access-list acl_inside permit ip any host 64.0.0.6
access-list acl_inside permit ip any host 64.0.0.7
access-list acl_inside permit ip any host 64.0.0.4
access-list acl_inside permit ip any host 64.0.0.2
access-list acl_inside permit ip any host 64.0.0.1
access-list 101 permit tcp any any eq telnet
access-list 101 permit tcp any any eq ftp
access-list 101 permit tcp any any eq www
access-list 101 permit tcp any any


Here is my code:


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
int aclCnt = 0;
string acl;
string aclname;
string aclfind;
string aclaction;
string remainder;
string line;

ifstream configData;
configData.open("configsource.txt", ios::in);

aclfind = "acl_inside";
if (configData.is_open())
{

for (int count = 0; count < 50; count = count + 1)
{
getline(configData, line) >> acl >> aclname >> aclaction;


if (acl == "access-list" && aclname == aclfind )
{

aclCnt = aclCnt + 1;
cout << acl << " " << aclname << line << endl;
}



}
cout << "Access-list: " << aclfind << " has " << aclCnt << " lines." << endl;
configData.close();
}
else
{
cout << "The file could not be opened." << endl;
}

system("pause");
return 0;
}


Here is my output:

access-list acl_inside
access-list acl_inside ip any any
access-list acl_inside ip any any
access-list acl_inside ip any host 64.0.0.6
access-list acl_inside ip any host 64.0.0.7
access-list acl_inside ip any host 64.0.0.4
access-list acl_inside ip any host 64.0.0.2
Access-list: acl_inside has 7 lines.
Press any key to continue . . .


If you look at the output, it is not as expected. The first line is incomplete and it is also missing the last line that ends in 64.0.0.1. I tried outputting the rest of the line a second time and that did not work. Any Ideas?
 
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	ifstream inFile( "c:/temp/test.txt" );

	if ( !inFile )
	{
		cout << "Error opening file!" << endl;
		return 1;
	}

	int aclCnt = 0;
	string acl;    
	string aclname;
	string aclfind( "acl_inside" );
	string aclaction;
	string remainder;
	string inStr;
	stringstream line;

	while ( getline( inFile, inStr ) )
	{
		line.str( "" );

		if ( line.bad() == true )
		{
			line.clear();
		}

		line << inStr;
		line >> acl;

		if ( acl == "access-list" )
		{
			line >> aclname >> aclaction;

			if ( aclname == aclfind )
			{ 
				aclCnt = aclCnt + 1;
				cout << acl << " " << aclname << line.str() << endl;
			}
		}
	}

	cout << "Access-list: " << aclfind << " has " << aclCnt << " lines." << endl;

	return 0;
}
 
uolj:

Your post was using manual input. I am reading this from a file. I have to use a getline to read the line, so when I use a getline the second time it is getting the next line. Sorry I didnt follow up. I may be wrong, but then again thats why Im here :)

cpjust, your code returned nothing.
 
CPJust,

I went back and did your suggestion

configData >> acl >> aclname >> aclaction;
getline(configData, remainder);


Everything is good to go now. Thanks to both. Im sure I will be back.
 
Ok, there is another problem I found. Ive changed the text file to test multiple access lists to look like so:

Code:
configsource.txt --
fixup protocol smtp 25
fixup protocol sqlnet 1521
fixup protocol tftp 69
names
object-group network GRP_INT_NETWORKS
  network-object host 10.1.1.1
  network-object host 192.168.0.0
object-group network test-group
  network-object host 10.10.10.10
  network-object 192.168.1.0 255.255.255.0
  network-object 192.168.50.0 255.255.255.0
  network-object 192.168.60.0 255.255.255.0
access-list 101 permit tcp any any eq www
access-list 102 permit tcp any any eq www
access-list 103 permit tcp any any eq www
access-list 104 permit tcp any any eq www
access-list 105 permit tcp any any eq www
access-list 106 permit tcp any any eq www
access-list 107 permit tcp any any eq www
access-list 108 permit tcp any any eq www
access-list 100 permit tcp any any eq www
access-list 110 permit tcp any any eq www

Now If I choose 101 as my search criteria nothing shows up but if I choose 102 it will count it. Im not sure what is going on here. Does this line:

configData >> acl >> aclname >> aclaction;

dump line by line? or the whole file and attempt to add to my variables without care for lines?

Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
	int aclCnt = 0;
	string acl;	
	string aclname;
	string aclfind;
	string aclaction;
	string remainder;
	string line;
    
    ifstream configData;
	configData.open("configsource.txt", ios::in);
	
	aclfind = "101";
	if (configData.is_open())
	{
                             
		for (int count = 0; count < 1000; count = count + 1)
		{
            configData >> acl >> aclname >> aclaction;
            
			
			
			
			if (acl == "access-list" && aclname == aclfind )
			{ 
                    getline(configData, remainder);
                    aclCnt = aclCnt + 1;
                    }
                    
        aclname = "";  
        acl = "";       
			
		}
		cout << "Access-list: " << aclfind << " has " << aclCnt << " lines." << endl;
		configData.close();
	}
	else
	{
		cout << "The file could not be opened." << endl;
	}

	system("pause"); 
	return 0;
}
 
That's strange. My code was working fine when I used your test input.
 
This is what I got for the final part. Basically I just output the remainder to sep variables. This will actually work out better because I will need to compare those variables later. The difference is it will show every line without the having to do a getline the second time. Also using sstream to ouput the line to a variable that can be parsed into more variables.

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

int main( )
{
	int aclCnt = 0;
	string acl;	
	string aclname;
	string aclfind;
	string aclaction;
	string rem1;
	string rem2;
	string rem3;
	string rem4;
	string rem5;
	string rem6;
	string rem7;
	string rem8;
	string rem9;
	string rem10;
	string line;
    
    ifstream configData;
	configData.open("configsource.txt", ios::in);
	
	aclfind = "101";
	if (configData.is_open())
	{
                             
		for (int count = 0; count < 1000; count = count + 1)
		{
            getline(configData, line);
            istringstream iss(line); 
			iss >> acl >> aclname >> aclaction >> rem1 >> rem2 >> rem3 >> rem4 >> rem5 >> rem6 >> rem7 >> rem8 >> rem9 >> rem10;
			
			
			if (acl == "access-list" && aclname == aclfind )
			{ 
                    cout << "Access-list: " << aclfind << " " << aclaction << " " << rem1  << " " << rem2  << " " << rem3   << " " << rem4   << " " << rem5 << " " << rem6   << " " << rem7  << " " << rem8 << " " << rem9 << " " << rem10 << endl;
                    aclCnt = aclCnt + 1;
                    }
                    
        aclname = "";  
        acl = "";       
			
		}
		cout << "Access-list: " << aclfind << " has " << aclCnt << " lines." << endl;
		configData.close();
	}
	else
	{
		cout << "The file could not be opened." << endl;
	}

	system("pause"); 
	return 0;
}
 
Code:
for (int count = 0; count < 1000; count = count + 1)
Does the file always have exactly 1000 lines?
 
No, Ill have to fix that also. I dont know how many line there will be. Ive seen where people do a while loop with EOF. Will that work?
 
If you look at my code, I use this to read all the lines of the file:
Code:
while ( getline( inFile, inStr ) )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top