Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   strtok help (http://www.go4expert.com/forums/strtok-help-t4363/)

rajib 19May2007 13:16

strtok help
 
Hi this is the code -

-----------------------------------------------------------
Code:

FILE *fp = fopen ("C:\\Temp\\in2.txt", "r");

fgets(line, sizeof(line), fp);

printf("line is %s\n", line);
sub_str = strtok_r(line, "~");
printf("Printing strings - %s \n", sub_str);

sub_str = strtok_r(NULL, "~");
printf("Next string - %s \n", sub_str);

--------------------------------------------------------

The line is -
ITem~G!!~~~~34

The O/P is -
ITem
G!!

HOW CAN I EXTRACT THE NULL VALUES ALSO IN BETWEEN? STRTOK() IGNORES IF THERE ARE NULL IN BETWEEN....

Please help. :mad:

DaWei 19May2007 16:04

Re: strtok help
 
There's no need to yell.

There are no NULL values in between. I presume you mean how does one extract all values in the string, which would include the "34". The '~' is not a NULL value, it is the chosen delimiter. You need to read the documentation of strtok. On the first call, you pass strtok a pointer to the string. It returns the first token, tokens being defined by the delimiters. To get the next token, you pass a NULL pointer rather than a pointer to the string. You get one additional token for each call. You stop when strtok returns a NULL pointer.

Logical reasoning, then, suggests that we put the subsequent calls in a loop and break the loop when strtok returns NULL.


All times are GMT +5.5. The time now is 02:47.