Saturday, 15 June 2013

c++ - splicing cstrings with strtok, only works on first execution of loop -



c++ - splicing cstrings with strtok, only works on first execution of loop -

i trying utilize strtok splice line read cstring individual strings. yes know done much more string objects, i'm not allowed utilize them. when code executes works on first line, continues work on same line every iteration of loop. file beingness read 'line' char array veriable, evidenced cout << line << endl; strtok , sec while loop go on split first line read in every single time. each line contains first_name last_name id , 6 grades. guess rewrite , have getline delimit @ white space , read in individual strings, know why strtok splicing first string on each successive iteration of loop?

this code segment, assume variables correctly declared elsewhere

while(!fin.eof()) {//while open fin.getline(line, 40, '\n'); cout << line << endl; ptr = strtok (line, " "); while(ptr != null) { if(c==0) sprintf(firstname, "%s", ptr); if(c==1) sprintf(lastname, "%s", ptr); if(c==2) sprintf(id, "%s", ptr); if(c==3) sprintf(grade1, "%s", ptr); if(c==4) sprintf(grade2, "%s", ptr); if(c==5) sprintf(grade3, "%s", ptr); if(c==6) sprintf(grade4, "%s", ptr); if(c==7) sprintf(grade5, "%s", ptr); if(c==8) sprintf(grade6, "%s", ptr); ptr = strtok (null, " "); if(ptr == null) break; c++; } }

you not reset variable c after leaving inner loop.

c++ c cstring strtok arrays

No comments:

Post a Comment