strtok exception handling

Discussion in 'C Programming' started by Neel, Sep 26, 2008.

  1. Neel

    Neel Guest

    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
     
    Neel, Sep 26, 2008
    #1
    1. Advertising

  2. Neel <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 26, 2008
    #2
    1. Advertising

  3. Neel

    Ian Collins Guest

    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.
     
    Ian Collins, Sep 26, 2008
    #3
  4. Neel

    Guest

    On Sep 26, 3:21 am, 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.


    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
     
    , Sep 26, 2008
    #4
  5. Neel

    Neel Guest

    It gives some error.
    Do I need to include any special header file for that?
     
    Neel, Sep 26, 2008
    #5
  6. Neel

    Ian Collins Guest

    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.
     
    Ian Collins, Sep 26, 2008
    #6
  7. Neel

    user923005 Guest

    On Sep 26, 1:21 am, 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?
    > 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.
     
    user923005, Sep 26, 2008
    #7
  8. Neel

    James Kuyper Guest

    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.
     
    James Kuyper, Sep 26, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Balgach
    Replies:
    2
    Views:
    587
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,953
    Pete Becker
    Apr 14, 2005
  3. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    523
  4. Peter
    Replies:
    34
    Views:
    1,999
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    261
Loading...

Share This Page