strtok exception handling

N

Neel

Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"

I use strtok to extract "Hello" and "World"

the code I use is

foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));

BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
I tried try-catch but didnt work.


Can anyone please help me with this issue?
Thanks in advance
 
K

Keith Thompson

Neel said:
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"

I use strtok to extract "Hello" and "World"

the code I use is

foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));

BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?

You'll have to show us a small, complete, compilable program that
demonstrates the problem.
I tried try-catch but didnt work.

Are you use a C++ compiler? comp.lang.c++ is down the hall, second
door on the left.
 
I

Ian Collins

Neel said:
Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"

I use strtok to extract "Hello" and "World"

the code I use is

foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));

BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?

Ever thought of checking the return value of strtok?
I tried try-catch but didnt work.
Well considering C doesn't have try-catch and the descendent language
that does certainly doesn't use them for asynchronous signals, that's
hardly surprising.
 
S

s0suk3

Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"

I use strtok to extract "Hello" and "World"

the code I use is

foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));

BUT If in case there's only one word, it gives me Segmentation Fault.

Sure, if it's only one word it will return NULL on the second call.
How can I handle this?
I tried try-catch but didnt work.

LOL! Did you try something like

try {
foo = atoi(strtok(data, " "));
}
catch (SegmentationFault e) {
printf("I caught a segfault! :)\n");
}

Sebastian
 
I

Ian Collins

Neel said:
It gives some error.
Do I need to include any special header file for that?

Including the context you are replying to in your posts would be a good
start.
 
U

user923005

Hi friends,
I 'm trying to extract values from a lines which are delimited by a
space
eg.
content of string is: "Hello World"

I use strtok to extract "Hello" and "World"

the code I use is

foo=atoi(strtok(data," "));
bar=atoi(strtok(NULL," "));

BUT If in case there's only one word, it gives me Segmentation Fault.
How can I handle this?
I tried try-catch but didnt work.

From:
http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
we have this:
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}


The while loop is due to this:
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
 
J

James Kuyper

Neel said:
It gives some error.
Do I need to include any special header file for that?

A very special one, I think - it was meant as a joke. The point is that
C doesn't have try/catch. C++ does, and for questions about C++ you
should go to comp.lang.c++. However, I can tell you what their answer
would be: C++ exceptions are also not usable for this purpose.

On the other hand, when strtok() has reached the end of the string you
asked it to tokenize, it returns a null pointer value. You're supposed
to use that fact to decide what to do next. If you try to actually use
that value as if it points to a string (for instance, by passing it to
atoi()), then you're going to get trouble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top