How does strtok work in this case?

  • Thread starter Thread starter TheMathNoob
  • Start date Start date
  • Tags Tags
    Work
Click For Summary

Discussion Overview

The discussion revolves around the use of the `strtok` function in C for parsing a specific data format from a file. Participants are addressing issues related to detecting the number of values in the first line of input and handling null pointers when parsing strings.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with a function that fails to detect if the first line of input has two values, which is necessary for creating a graph.
  • Another participant suggests using online resources for answers, providing a link to a tutorial on `strtok`.
  • Some participants express frustration that their code is not detecting null values as expected.
  • There is a mention that NULL is an invalid pointer by definition, which may be contributing to the issues faced.
  • A participant points out a potential error in the provided example code regarding string termination, suggesting a correction to ensure proper memory allocation.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the issues with `strtok`, and multiple competing views on the problem and its solutions remain. The discussion is unresolved regarding the correct implementation and handling of null values.

Contextual Notes

Limitations include potential misunderstandings of pointer behavior in C, as well as issues related to string termination and memory management that have not been fully addressed.

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
TheMathNoob said:
My code is not detecting the null
Maybe because NULL is an invalid pointer (by definition).
 
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);
 
A slight error: Your first line (char str[5]="jk kj";) has no room for a string terminator. You should use char *str="jk kj"; (or char str[]="jk kj";).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K