Using strtok()

Using strtok()

Post by Roger » Mon, 24 Jul 1995 04:00:00



Hello all,

I'm trying to parse a string in the particular format:

test: add write delete

I want to be able to separate the "test" and parse the rest into
individual words.  

bla[] = {"test: add write delete"};
tmp = strtok(bla, ":");
tmp = strtok(NULL, " ");

The NULL seems to cause core dumps.  I've tried this on Borland C++, and
it works fine.  What's the problem?  Are there any other ways of avoiding
this problem?

 
 
 

Using strtok()

Post by Rene Pijlma » Mon, 24 Jul 1995 04:00:00



>Hello all,

>I'm trying to parse a string in the particular format:

>test: add write delete

>I want to be able to separate the "test" and parse the rest into
>individual words.  

>bla[] = {"test: add write delete"};
>tmp = strtok(bla, ":");
>tmp = strtok(NULL, " ");

>The NULL seems to cause core dumps.  I've tried this on Borland C++, and
>it works fine.  What's the problem?  Are there any other ways of avoiding
>this problem?

No core dumps here (Linux 1.2.8, gcc 2.5.8):

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>

    int main(void)
    {
        char * tmp;

        char bla[] = {"test: add write delete"};

        tmp = strtok(bla, ":");
        printf("%s\n", tmp);
        tmp = strtok(NULL, " ");
        printf("%s\n", tmp);

        return EXIT_SUCCESS;
    }

    Running this program says:

        test
        add

Maybe you can post a source, a list of the software and libraries that you're
using etc.

Regards,
--
-Ren\'e Pijlman-                      _-^^-_

                                   //   _ -  /
On no account allow a Vogon       || \  -  /\\
to read poetry at you.            ||  \___/ ||
- Hitchiker's Guide to the Galaxy ||       //|\
------------------------------------------------

 
 
 

Using strtok()

Post by Paul Beardsel » Tue, 25 Jul 1995 04:00:00



> Hello all,

> I'm trying to parse a string in the particular format:

> test: add write delete

> I want to be able to separate the "test" and parse the rest into
> individual words.  

> bla[] = {"test: add write delete"};
> tmp = strtok(bla, ":");
> tmp = strtok(NULL, " ");

> The NULL seems to cause core dumps.  I've tried this on Borland C++, and
> it works fine.  What's the problem?  Are there any other ways of avoiding
> this problem?

Don't forget:
#include <stdlib.h>

--
Paul Beardsell                          SAM Business Systems Ltd
~~~~~~~~~~~~~~                          21 Finn House, Bevenden St,


 
 
 

Using strtok()

Post by Richard Lee Bauma » Wed, 26 Jul 1995 04:00:00


|> Hello all,
|>
|> I'm trying to parse a string in the particular format:
|>
|> test: add write delete
|>
|> I want to be able to separate the "test" and parse the rest into
|> individual words.  
|>
|> bla[] = {"test: add write delete"};
|> tmp = strtok(bla, ":");
|> tmp = strtok(NULL, " ");
|>
|> The NULL seems to cause core dumps.  I've tried this on Borland C++, and
|> it works fine.  What's the problem?  Are there any other ways of avoiding
|> this problem?
|>
|>

Works fine for me (no core dumps) under Sun C++ 4.0.1 on a BSD Unix system.
What compiler and operating system are you using?

--
Richard Baumann                         (805) 447-8391
International Clinical Safety
AMGEN
---------------------------------------------------------------
The opinions expressed herein are strictly those of the author.

 
 
 

Using strtok()

Post by Kari E. Hurt » Thu, 27 Jul 1995 04:00:00



?No core dumps here (Linux 1.2.8, gcc 2.5.8):

?    #include <string.h>
?    #include <stdlib.h>
?    #include <stdio.h>

?    int main(void)
?    {
?      char * tmp;

?      char bla[] = {"test: add write delete"};

?      tmp = strtok(bla, ":");
?      printf("%s\n", tmp);
?      tmp = strtok(NULL, " ");
?      printf("%s\n", tmp);

?      return EXIT_SUCCESS;
?    }

?    Running this program says:

?        test
?        add

?Maybe you can post a source, a list of the software and libraries that you're
?using etc.

It _should_ core dump or your constant strings are writable.
Notice that strtok destroys string pointed by first argument.

[ Comment cc'ed to Rene Pijlman ]

 
 
 

Using strtok()

Post by Dan Merc » Fri, 28 Jul 1995 04:00:00




: ?No core dumps here (Linux 1.2.8, gcc 2.5.8):

: ?    #include <string.h>
: ?    #include <stdlib.h>
: ?    #include <stdio.h>

: ?    int main(void)
: ?    {
: ?    char * tmp;

: ?    char bla[] = {"test: add write delete"};

: ?    tmp = strtok(bla, ":");
: ?    printf("%s\n", tmp);
: ?    tmp = strtok(NULL, " ");
: ?    printf("%s\n", tmp);

: ?    return EXIT_SUCCESS;
: ?    }

: ?    Running this program says:

: ?        test
: ?        add

: ?Maybe you can post a source, a list of the software and libraries that you're
: ?using etc.

: It _should_ core dump or your constant strings are writable.
: Notice that strtok destroys string pointed by first argument.

: [ Comment cc'ed to Rene Pijlman ]

bla is an automatic variable array initialized from the constant
string "test: add write delete".  It is NOT a constant string.

--
Dan Mercer                                            ClearSystems, Inc

======================================================================
All opinions expressed are my own and do not reflect the opinions of
my employer or my employer's clients,  in particular 3M Company.
All advice or software offered or presented is provided As Is with no
warranty either expressed or implied.  Follow at your own risk.
Objects in the mirror are closer than they appear.

 
 
 

1. HELP:How to parse using strtok()?

Hi, I'd like to build a small C parser within my program. I assume that I will
need strtok(). I've looked at 'man strtok' but there isn't much information.
Do you know if there are any samples available over the web. Some sources for
code would be useful.

I can't afford the time to learn lex/yacc now, hence my question. Thanks very
much.

2. Good firewall material?

3. Patch: linux-2.5.8-pre1: Complete strtok --> strsep conversion

4. Newbie needs help with Kernel compilation

5. xlc does not like strtok()?

6. Video Tearing - Problem Fine Tuning X

7. strtok() problem or gcc ?

8. GPIB for Linux?

9. strtok unsafe?

10. strtok() problem or gcc ?

11. strtok, SIGSEGV, and core dumps

12. strtok --> strsep in framebuffer drivers

13. strtok function