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

  1. Newsgroups: alt.hackers
  2. Path: sparky!uunet!psinntp!dg-rtp!mirage!welshm
  3. From: welshm@mirage.rtp.dg.com (Matt Welsh)
  4. Subject: Re: not a test
  5. Message-ID: <1992Jul24.123210.12601@dg-rtp.dg.com>
  6. Sender: usenet@dg-rtp.dg.com (Usenet Administration)
  7. Approved: by 3 out of 4 dentists
  8. Date: Fri, 24 Jul 92 12:32:10 GMT
  9. References: <1992Jul20.185304.837@amhux2.amherst.edu> <ik89NB1w164w@kzdoos.hacktic.nl>
  10. Organization: Data General Corporation, RTP, NC.
  11. Lines: 43
  12.  
  13. In article <ik89NB1w164w@kzdoos.hacktic.nl> koos@kzdoos.hacktic.nl (Koos van den Hout) writes:
  14.  
  15. >ObHack : In a program I'm working on I need to parse lines that are in
  16. >the form :
  17. ><name> <value> <value> <string> [<string>...]
  18. >So 'group_A 0 0 aaa' is valid but 'group_B 10 0 bb1 bb2 bb3' also.
  19. >Problem : How to parse this when the complete line is already stored in memory?
  20. >Solution:
  21. >
  22. >The line is stored in char buf[512];
  23. >
  24. >int nm,inc;
  25. >
  26. >sscanf(buf,"%s %d %d%n",name,&val1,&val2,nm);
  27. >/* do something with the name and val1 and val2 */
  28. >
  29. >while(sscanf(&buf[nm],"%s%n",member,&inc)==1){
  30. >   nm+=inc;
  31. >   /* do something with member */
  32. >}
  33. >
  34.  
  35. There's a much easier way... use strtok().
  36.  
  37. sscanf(buf,"%s %d %d %[^\n]",name,&val1,&val2,buf2);
  38. i=1;
  39. strings[0]=strtok(buf2," \t");
  40. while (strings[i]=strtok(NULL," \t")) i++;
  41.  
  42. Of course this assumes that you've got an array of char *'s large 
  43. enough, but it would be just as easy to use linked lists here.
  44.  
  45. >Works like a charm....
  46.  
  47. >| Koos van den Hout                          E-mail koos@kzdoos.hacktic.nl |
  48. >| Student Computer Science                   Fido Sysop @ 2:500/101.11012  |
  49. >| SysOp BBS Koos z'n Doos (+31-3402-36647)  300/1200/2400N81 ANSI/AVT/TTY  |
  50. >| International E-mail costs me 10 times as much as a followup ...         |
  51.  
  52.  
  53. Matt Welsh    welshm@dg-rtp.dg.com        ...!mcnc!rti!dg-rtp!welshm
  54. UNIX-SQA, Data General Corporation RTP        Office: +1 919 248 6070
  55.   "Do not taunt Happy Fun Ball."
  56.