Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!husc6!bloom-beacon!think!ames!ptsfa!ihnp4!chinet!rlk From: rlk@chinet.UUCP (Richard Klappal) Newsgroups: comp.unix.wizards Subject: Re: scanf quiz question Message-ID: <1320@chinet.UUCP> Date: Sun, 19-Jul-87 11:05:09 EDT Article-I.D.: chinet.1320 Posted: Sun Jul 19 11:05:09 1987 Date-Received: Sun, 19-Jul-87 21:03:51 EDT References: <1220@ccicpg.UUCP> Reply-To: rlk@chinet.UUCP (Richard Klappal) Organization: Chinet - Public Access Unix Lines: 36 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 > ..... >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 Since the number of strings following the '#' may be variable, I would use strtok (see strings(3)), if you have it. { if not, look thru the net.sources, or mod.sources, archives for Henry Spencer's strings package. } ... fgets(stream, MAXLINE, buffer); strcpy(s1,strtok(buffer," ")); strcpy(s2,strtok(NULL,"#")); strcpy(s3,strtok(NULL,"\n")); I have not included any error checking in this example, but you should do so since strtok() returns NULL when no token remains. -- --- UUCP: ..!ihnp4!chinet!uklpl!rlk || MCIMail: rklappal || Compuserve: 74106,1021 ..!ihnp4!ihu1h!rlk ---