• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Parsing an int from the end of a string in c++

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

iLoki

Member
Joined
Dec 8, 2008
Hey,
I'm writing a simple log parser to track /rolls in LotRO. It's a fairly straightforward application, it reads in lines from the input file as a string, and checks it for the player name, and the keyword " rolled (100)". I am having trouble pulling the integer from the end of the string. The format of the line is always as follows:

(using my main toons name)
Code:
Falthelion rolled (100): 34

I have tried tokenizing, as well as a stringstream, but I find they either don't work or I don't fully understand the proper method of implementation. If it's helpful, here's a portion of the code:

Code:
void readRoll(string file, string charName, string saveFile){
	int rollTotal=0, numRoll=0, rollAvg=0, roll=0;

	ifstream OpenFile(file.c_str()); // open up the log file for parsing
	ofstream SaveFile(saveFile.c_str(),ios::app); // open up the data file in append mode, so that we don't overwrite any existing data.
	string line; //string variable we will use for parsing

	while(!OpenFile.eof()){ //loop through file until EOF character is reached.
		getline(OpenFile,line,'\n');
		if(line.find(charName + " rolled (100)") != string::npos){//if character name is in line, write to console AND file.
			cout << line << endl;
			numRoll++;
			if(!SaveFile.is_open()){ //make sure that the SaveFile is open, exit if it is not.
				cout << "Error! " << saveFile << " could not be opened!" << endl;
				}else{
				SaveFile << line << endl; //write line to the file.
			}		
		}else if(line.find("rolled (100)") != string::npos){ //if character name isn't in the line, write to file, but not console.
			if(!SaveFile.is_open()){ //make sure that the SaveFile is open, exit if it is not.
				cout << "Error! " << saveFile << " could not be opened!" << endl;
			}else{
				SaveFile << line << endl;
			}
		}
		 
	}	
	OpenFile.close();//close the files, just to be safe.
	SaveFile.close();
}


Can somebody give me some tips on reading integers from strings?

Thanks in advance
 
Got it... ended up tokenizing with the stringstream and then testing the assignment of the token as an int (case with atoi()) and skipping any 'invalid' values through a try/catch block.
 
Isn't that .NET?

iLoki looks like he's programming C++ Win32

The concept of regular expressions are the same regardless of the code used. They simply match on patterns and remove and/or store pieces of the string you are parsing. The above block of code could be condensed down to less than 10 lines with a different approach.
 
I'm planning on doing a GUI version of this. I'll probably Regular expressions, as I actually need to teach myself how to use them :)

The rest from there, would just be figuring out the GUI aspects of it, deciding on my layout, how to handle the open/save dialogs and how to display the data that I've read and/or calculated.
 
Last edited:
I did the GUI version in C#, to kinda learn how to use Visual Studio for graphics, and how to handle all of the event handlers, and I also went ahead and figured out how to get what I wanted out of Regular Expressions. I really, really like searching with Regular Expressions, and 90% of my dealings with strings will be through Regex now.

Anyways, if you guys are interested, check it out:

http://my.lotro.com/wpcloki/
 
Back