home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / alt / hackers / 1157 < prev    next >
Encoding:
Text File  |  1992-07-23  |  2.0 KB  |  68 lines

  1. Newsgroups: alt.hackers
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!eff!ibmpcug!kate.ibmpcug.co.uk!dylan
  3. From: dylan@ibmpcug.co.uk (Matthew Farwell)
  4. Subject: Re: not a test
  5. Organization: The IBM PC User Group, UK.
  6. Date: Fri, 24 Jul 1992 08:48:17 GMT
  7. Approved: Welcome to the house of fun now you've come of age.
  8. Message-ID: <1992Jul24.084817.13422@ibmpcug.co.uk>
  9. References: <1992Jul23.075512.21400@cbfsb.cb.att.com> <1992Jul23.101031.27724@jarvis.csri.toronto.edu> <0JNHGMG@taronga.com>
  10. Lines: 56
  11.  
  12. In article <0JNHGMG@taronga.com> peter@taronga.com (Peter da Silva) writes:
  13. >In article <1992Jul23.101031.27724@jarvis.csri.toronto.edu> flaps@dgp.toronto.edu (Alan J Rosenthal) writes:
  14. >>good god, please just use strtok().  Get to know your friendly neighbourhood C
  15. >>library.  This is EXACTLY what strtok() is for.
  16. >Good god, please don't use strtok. It's inherently non-reentrant and it's not
  17. >even usable in cooperative threads. Define a new function that operates on a
  18. >string handle or a string file, but let strtok die a well deserved death.
  19.  
  20. As usual, I'm going to say go and look at the library functions in the
  21. c-news distribution, particularly split(3).
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #define EXPECTED    4        /* expected number of fields */
  27.  
  28. char buf[512] = "group_B 10 0 bb1 bb2 bb3";
  29.  
  30. extern int split( char *, char **, int, const char * );   /* from c-news */
  31.  
  32. int main()
  33. {
  34.     int i;
  35.     char **flds = NULL;
  36.     char *dummy;
  37.     int numfields;
  38.  
  39.         /* just count the number of fields first time round */
  40.     numfields = split(buf, &dummy, 1, " ");
  41.  
  42.     if (numfields < EXPECTED) {
  43.         fprintf(stderr, "not enough fields\n");
  44.         exit(1);
  45.     }
  46.  
  47.     flds = (char **)malloc(numfields*(sizeof (char **)));
  48.     if (flds == NULL) {
  49.         fprintf(stderr, "malloc failed\n");
  50.         exit(1);
  51.     }
  52.  
  53.     if (split(buf, flds, numfields, " ") != numfields) {
  54.         fprintf(stderr, "sorry - completely confused\n");
  55.         exit(1);
  56.     }
  57.  
  58.     for (i=0 ; i<numfields ; i++)
  59.         printf("|%s|\n", flds[i]);
  60.  
  61.     return 0;
  62. }
  63.  
  64. Dylan.
  65. -- 
  66. It is no coincidence that in no known language does the phrase 'As
  67. pretty as an Airport' appear -- Douglas Adams
  68.