Hi,
I've been developing some code that parses output reports and re-reports on information that is of interest. I utilize strtok quite heavily in this program and I have run into the situation where my final strtok call may or may not return some info. Depending on if there is info returned will dictate my branch in an IF statement. Code snipit:

while (fgets(buffer, 133, ofp) != NULL)
{
if (!strncmp(&buffer[1], "Queue:", 6))
{
memset(oqnamlib,'\0',sizeof(oqnamlib));
if (strncmp(&buffer[10]," ",10))
{
strncpy(que,&buffer[10],10);
token=strtok(buffer," ");
token=strtok(NULL," :");
token=strtok(NULL,":");
token=strtok(NULL," :");
len = strlen(&token[0]);
if (len <= 0)

If the last call to strtok does not find anymore info, i.e. token becomes NULL, then if I do the strlen call the code blows up at runtime. token is defined as char *token. I have tried the strlen command with token, *token (doesn't compile), &token and &token[0]. Any suggestions?
{

Recommended Answers

All 2 Replies

Check your warnings. You'll almost certainly be getting them on every line that you've passed your buffer/token. Remove the & sign and it should work correctly.

you could use a while statement

int len = 0;
token = strtok(buffer," ");
while( token )
{
   len = strlen(token);
   token = strtok(NULL,":");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.