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