Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok exception handling

Reply
Thread Tools

strtok exception handling

 
 
Neel
Guest
Posts: n/a
 
      09-26-2008
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-26-2008
Neel <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-26-2008
Neel wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      09-26-2008
On Sep 26, 3:21*am, Neel <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Neel
Guest
Posts: n/a
 
      09-26-2008
It gives some error.
Do I need to include any special header file for that?
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-26-2008
Neel wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      09-26-2008
On Sep 26, 1:21*am, Neel <(E-Mail Removed)> wrote:
> 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/c...ng/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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-26-2008
Neel wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Problems with strtok() returning one too many tokens... Adam Balgach C++ 2 11-28-2004 01:12 AM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments