strtok exception handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neel

    strtok exception handling

    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
  • Keith Thompson

    #2
    Re: strtok exception handling

    Neel <a.k.vora@gmail .comwrites:
    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_Keit h) kst-u@mib.org <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"

    Comment

    • Ian Collins

      #3
      Re: strtok exception handling

      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.

      Comment

      • s0suk3@gmail.com

        #4
        Re: strtok exception handling

        On Sep 26, 3:21 am, Neel <a.k.vora@gmail .comwrote:
        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(dat a, " "));
        }
        catch (SegmentationFa ult e) {
        printf("I caught a segfault! :-)\n");
        }

        Sebastian

        Comment

        • Neel

          #5
          Re: strtok exception handling

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

          Comment

          • Ian Collins

            #6
            Re: strtok exception handling

            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.

            Comment

            • user923005

              #7
              Re: strtok exception handling

              On Sep 26, 1:21 am, Neel <a.k.v...@gmail .comwrote:
              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:

              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.

              Comment

              • Richard Heathfield

                #8
                Re: strtok exception handling

                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?
                By taking advantage of the fact that strtok returns NULL if no more tokens
                exist.

                p = strtok(data, " ");
                if(p != NULL)
                {
                /* okay, got a token, keep going */
                foo = strtol(p, &endptr, 10);
                if(endptr p)
                {
                /* okay, foo populated - keep going */
                p = strtok(NULL, " ");
                if(p != NULL)
                {
                /* okay, got a token, keep going */
                bar = strtol(p, &endptr, 10);
                if(endptr p)
                {
                /* okay, bar populated, keep going */

                Comment

                • James Kuyper

                  #9
                  Re: strtok exception handling

                  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.

                  Comment

                  Working...