Monday, July 27, 2009

How to handle an asterisk at the end of line by itself as indication of end of section of file in c++?

I am reading a file, which has two sections. first section has names and ids and second section has ids and score. sections are seprated by asterisk.


example


joe smith 23


alice west 45


*


23 78 89 9 90 -1


45 78 98 98 -1


i am reading first and last name as strings id's as intergers.


in a struct variable.


but when I put files together and try to read using * as a sentinel for first section. it gives me segmentation fault. any one knows how to handle it?





void read(ifstream %26amp; infile, player_rec league[],int %26amp; size)


{








string firstname,lastname;


int id;


infile%26gt;%26gt;firstname%26gt;%26gt;lastname%26gt;%26gt;id;


while( firstname != "*")


{


fixname(firstname);


fixname(lastname);


league[size].fname=firstname;


league[size].lname=lastname;


league[size].idnum=id;


size++;


infile%26gt;%26gt;firstname%26gt;%26gt;lastname%26gt;%26gt;id;








}


}

How to handle an asterisk at the end of line by itself as indication of end of section of file in c++?
when do you get the segmentation fault?


there are two reasons i suppose you get it.


first:


if you read it like this, maybe the problem is that when you encounter the "*", you continue reading a lastname and id that's not there.


second:


if it's a string and not a character, then comparing it with the "==" or "!=" operator isn't the right choice. use string.compare for it.


i hope i could help...
Reply:cant understand


No comments:

Post a Comment