How does strtok work in this case?

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
  • Tags Tags
    Work
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 1K views
TheMathNoob
Messages
189
Reaction score
4

Homework Statement


I have a file which has data set in this way
1
2 3
4 7
1 2
I have to make a graph with those values considering that the value of the first line corresponds to the vertices and the next two values per line to the edges. I am having troubles with a function that doesn't detect if the first line has two values or not. If it does, then it should displayed an error.

Homework Equations

[/b]
Code:
int ParseN(char line[])
{
    char* first;
    char* second;
    int true=1;
    int numberVertices=0;
    char* pt=strtok(line," -");
    printf("%s",pt);
    char* pt2=strtok(NULL," -");
    printf("%s",pt2);
    if(pt2==NULL) // in this case when I test my file with just one value in the first line, it doesn't detect the NULL
    {
        puts("yes");
    }
    else
        puts("no");
    return 1;
}

by the way sorry for not following the directions in the previous post. Now I understand what I have to do

The Attempt at a Solution

 
Last edited by a moderator:
Physics news on Phys.org
Svein said:
Maybe because NULL is an invalid pointer (by definition).
even the code in the website that you gave me doesn't work
Code:
char str[5]="jk kj";
    char s=" ";
    char* token;
    int counter=0;
    token = strtok(str,s);

      while( token != NULL )
      {
         printf( " %s\n", token );
         token = strtok(NULL,s);
      }    free(str);