Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!clyde!bellcore!faline!ulysses!sfmag!sfsup!dcm From: dcm@sfsup.UUCP (David C. Miller, consultant) Newsgroups: comp.unix.wizards Subject: Re: scanf quiz question Message-ID: <1666@sfsup.UUCP> Date: Wed, 22-Jul-87 08:26:41 EDT Article-I.D.: sfsup.1666 Posted: Wed Jul 22 08:26:41 1987 Date-Received: Sat, 25-Jul-87 09:09:16 EDT References: <1220@ccicpg.UUCP> Reply-To: dcm@sfsup.UUCP (David C. Miller, consultant) Organization: AT&T Information Systems, Summit, N. J. Lines: 48 In article <1220@ccicpg.UUCP> cracraft@ccicpg.UUCP (Stuart Cracraft) writes: >Suppose you have a line from a file such as: > >str1 str2 str3 str4 ... strN # strN+1 ... strN+I > ^ ^ ^ > | | | > Store in s1 | | > Store in s2 Store in s3 > >The above line consists of three fields: > 1) str1 > 2) str2 ... strN (where N is up to 10) > 3) strN+1 ... strN+I (where I is up to 10) >(strX represents an arbitrary-length string, up to 20 characters.) > >You want to use scanf to parse this line, storing >each of the three fields in its own variable. The obvious > > scanf("%s %[^#] %s",s1,s2,s3) > >successfully parses s1 & s2, but doesn't correctly parse s3. > >How do you use scanf to do it? > > Stuart There are two problems with the format you used. 1. There is no provision for disposing of the '#'. 2. Field 3 is specified as ONE space separated field. Correction follows: scanf("%s %[^#]# %[^\n]\n", s1, s2, s3); ^ ^ ^ | | +---- Clean up newline | +-------- Field 3 is everything up to newline +------------- Clean up # The newline may be replaced with any other character you may use to designate end-of-line. Hope this helps. Dave David C. Miller, consultant Comm Addresses: Paperware: AT&T Information Systems,190 River Road,Summit,NJ 07901 Liveware: (201) 522-6107 Software: {allegra,burl,cbosgd,clyde,ihnp4,ulysses}!sfsup!dcm