0

I am trying to use strtok to splice a line read into a cstring into individual strings. Yes I know this could be done much more easily with string objects, but I'm not allowed to use them. When this code executes it works perfectly on the first line, then it continues to only work on that same line for every iteration of the loop. The file is being successfully read into the 'line' char array veriable, as evidenced by cout << line << endl; However strtok and the second while loop continue to split the first line read in every single time. Each line contains a first_name last_name ID and six grades. I guess I will just rewrite it and have getline delimit at white space and read in individual strings, but does anyone know why strtok is only splicing the first string on each successive iteration of the loop?

this is a code segment, assume all variables are 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++;
        }
}
user1768079
  • 683
  • 3
  • 10
  • 18
  • Your c++ is very funny :-( – qPCR4vir Feb 13 '13 at 08:15
  • What do you mean by that? I'm new to c++. Only real programming I've done is with python. My school teaches c++, and now all of a sudden one of my teachers decides he does not like string objects and forces us to use cstrings for no apparent reason that I can see. So if you mean my use of cstrings, and sprintf and strtok type functions rather than stream functions and string objects, I agree. It is very odd. – user1768079 Feb 13 '13 at 08:35
  • Ok, I understand. You are exercising C not C++, but you literally write c++ :-) mixing styles. Apart from that the if solution is somewhat too “manual” (use switch/break ?)… or just consecutive parse…? I guess now you just want it to work… – qPCR4vir Feb 13 '13 at 08:57
  • `strtok` is broken, and should not be used, ever. It maintains global state, which makes any program using it unmaintainable. – James Kanze Feb 13 '13 at 09:16
  • Also, `while ( fin.eof() )` is incorrect, as is trying to use the results of `fin.getline` without verifying that it succeeded. – James Kanze Feb 13 '13 at 09:18
  • And finally, why are you not allowed to use `std::string`? If this is a course, then you shouldn't have seen `char[]` until long after you've seen `std::string`. – James Kanze Feb 13 '13 at 09:19
  • I don't know why we are not allowed to use std::string. Possibly to make it more difficult? I'm not sure. It is a second level CS course, the book we use is for C++, and we are expected to use C++ not C. We have not yet learned about objects, and object oriented programming so it is possible the teacher does not want us using std::string without understanding classes and ADT, but again, it explicitly says in the instructions not to use string objects, or methods related to them. Believe me, I would much rather use string objects as I am more familiar with them. – user1768079 Feb 13 '13 at 11:49

2 Answers2

2

You do not reset the variable c after leaving the inner loop.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

As @Joachim Pileborg the principal problem is reset c. It could be more explicit to initialize it before enter the inner loop c=0; and to add and else for each if except for the last.

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32