Parsing file using space delimiter issue

Hi, I have a file in the following format :

**********************************************************

Startpoint: reg_4A

Endpoint: reg_49A

slack (VIOLATED) -0.157



Startpoint: reg_12A

Endpoint: reg_14A

slack (VIOLATED) -0.12



**********************************************************

I need to parse the above values and store it in the class data members after creating two objects of the class.

Can someone help in checking the file parsing logic ?

Code:

int main()

{

timing t1,t2;

std::string str1 ("Startpoint:");

std::string str2 ("Endpoint:");

std:: string str3 ("slack")

int first_occurence_startpoint =1;

int first_occurence_endpoint =1;

int first_occurence_slack =1;

std::ifstream file(timing.txt);

if (file.is_open()) {

std::string line;

while (getline(file, line)) {

vector<string> sep = split(line, ' ');

if(str1.compare(sep[0])==0 && first_occurence_startpoint )

{ first_occurence_startpoint =0;

t1.startname= sep[1];

}

if( str1.compare(sep[0])==0 && !first_occurence_startpoint )

t2.startname =sep[1];

if(str2.compare(sep[0])==0 && first_occurence_endpoint )

{ first_occurence_endpoint =0;

t1.endname= sep[1];

}

if( str2.compare(sep[0])==0 && !first_occurence_endpoint )

t2.endname =sep[1];

if(str3.compare(sep[0])==0 && first_occurence_slack )

{ first_occurence_slack =0;

t1.slack_value= sep[2];

}

if( str3.compare(sep[0])==0 && !first_occurence_slack )

t2.slack_value =sep[2];

}

}

file.close();

std::vector<timing*> total;

total.push_back(&t1);

total.push_back(&t2);

for ( auto object_iterator : total ) {

object_iterator->print();

}

total.clear(); //This will destroy all the objects

vector<timing>().swap(total); // This will create an empty vector with no memory allocated and swap it with totoal vector, effectively deallocating the memory.

}
Last edited on
first: http://www.cplusplus.com/articles/jEywvCM9/

and for your homework, I think you are expected to solve this naively, and assume that every value you retrieve has no spaces, so you can exclusively use the >> operator. Otherwise if you are explicitly told to include values with spaces, the best solution is to use the find function for the colon, and do a primitive loop, push every letter into another sting, and have a boolean for whether the previous letter is a space (this value starts off true). And depending if the order of keys are not hardcoded, you should use a std::map.

Also double thread posting for the same problem is a waste of everyones time.
http://www.cplusplus.com/forum/beginner/262740/
Topic archived. No new replies allowed.