Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple question - please help!

Status
Not open for further replies.

GusBrown

Programmer
Nov 24, 2000
38
GB
Hi all,

I'm using strtok to tokenise a string, e.g. "1;2;3;4;5" using ";" as the delimiter.

What I want is to say: int i = strtok( myString, ";" )
...but I obviously can't do that. If I cast the result of strtok as an int it doesn't give the result I want.

So the question is, how can I turn the results of strtok e.g. "1" into an int, 1 ???

Hope that makes some sense,

Gus
 
Hi, try the following program, this may solve your problem.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void )
{
char *str, *tok;
int i;
str = strdup( &quot;1:2:3:4:5:6:7:&quot; );
printf( &quot;%s\n&quot;, str);
tok = strtok(str, &quot;:&quot;);
i = tok != NULL ? atoi(tok) : 0;
while(i)
{
printf( &quot;\n..........%d&quot;, i);
tok = strtok(NULL, &quot;:&quot;);
i = tok != NULL ? atoi(tok) : 0;
}
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top