home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: alt.hackers
- Path: sparky!uunet!usc!sol.ctr.columbia.edu!eff!ibmpcug!kate.ibmpcug.co.uk!dylan
- From: dylan@ibmpcug.co.uk (Matthew Farwell)
- Subject: Re: not a test
- Organization: The IBM PC User Group, UK.
- Date: Fri, 24 Jul 1992 08:48:17 GMT
- Approved: Welcome to the house of fun now you've come of age.
- Message-ID: <1992Jul24.084817.13422@ibmpcug.co.uk>
- References: <1992Jul23.075512.21400@cbfsb.cb.att.com> <1992Jul23.101031.27724@jarvis.csri.toronto.edu> <0JNHGMG@taronga.com>
- Lines: 56
-
- In article <0JNHGMG@taronga.com> peter@taronga.com (Peter da Silva) writes:
- >In article <1992Jul23.101031.27724@jarvis.csri.toronto.edu> flaps@dgp.toronto.edu (Alan J Rosenthal) writes:
- >>good god, please just use strtok(). Get to know your friendly neighbourhood C
- >>library. This is EXACTLY what strtok() is for.
- >Good god, please don't use strtok. It's inherently non-reentrant and it's not
- >even usable in cooperative threads. Define a new function that operates on a
- >string handle or a string file, but let strtok die a well deserved death.
-
- As usual, I'm going to say go and look at the library functions in the
- c-news distribution, particularly split(3).
-
- #include <stdio.h>
- #include <stdlib.h>
-
- #define EXPECTED 4 /* expected number of fields */
-
- char buf[512] = "group_B 10 0 bb1 bb2 bb3";
-
- extern int split( char *, char **, int, const char * ); /* from c-news */
-
- int main()
- {
- int i;
- char **flds = NULL;
- char *dummy;
- int numfields;
-
- /* just count the number of fields first time round */
- numfields = split(buf, &dummy, 1, " ");
-
- if (numfields < EXPECTED) {
- fprintf(stderr, "not enough fields\n");
- exit(1);
- }
-
- flds = (char **)malloc(numfields*(sizeof (char **)));
- if (flds == NULL) {
- fprintf(stderr, "malloc failed\n");
- exit(1);
- }
-
- if (split(buf, flds, numfields, " ") != numfields) {
- fprintf(stderr, "sorry - completely confused\n");
- exit(1);
- }
-
- for (i=0 ; i<numfields ; i++)
- printf("|%s|\n", flds[i]);
-
- return 0;
- }
-
- Dylan.
- --
- It is no coincidence that in no known language does the phrase 'As
- pretty as an Airport' appear -- Douglas Adams
-