Hi all,
I'm trying to parse a text file like this:
Code:
i:-15 # Comment Line 1
u:30 # Commnet Line 2
d:5/2009 # Comment Line 3
$:154.72 # Comment Line 4
where the first char is the type, the number or date is some primitive or object, and obviously everything follwoing the '#' is a comment
I'm using this method right now:
Code:
parseFile( ifstream& if, Part& p, string fileName ){
// ommitting the obvious stuff here...
string temp;
char type;
while( getline( if, temp, ':' ){
// Declare temp primitives and objects here:
int tempInt;
unsigned int tempUint;
// etc...
switch ( type ){
case 'i' :
getline( if, intString, '#' );
intStream << intString;
if( !( intStream >> tempInt ).fail() ){
p.setInt( tempInt );
}
// etc
Problem is, if there's more than one int, or whatever in Part p, it overwrites the tempInt or whatever ( respectively ).
This needs to be a very general solution. I know I can't update some Part p within the function the way I have it right now. I just need a fresh idea or an extra set of eyeballs on how to do this.
Any ideas are most appreciated.
Thank you
EDIT:
for clarity, suppose Part looks like this:
Code:
class Part{
public:
// ...
private:
int firstInt;
int secondInt;
};
case 'i' in the switch can't handle both of these like this. I need to somehow set things up so that the file parser can return a specific type.