Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!whuxl!whuxlm!akgua!gatech!seismo!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.UUCP Newsgroups: net.unix,net.lang.c Subject: Re: mixing scanf and gets Message-ID: <2336@brl-smoke.ARPA> Date: Thu, 3-Apr-86 13:37:14 EST Article-I.D.: brl-smok.2336 Posted: Thu Apr 3 13:37:14 1986 Date-Received: Sat, 5-Apr-86 12:24:44 EST References: <1740@ittatc.ATC.ITT.UUCP> Reply-To: gwyn@brl.ARPA Distribution: net Organization: Ballistic Research Lab (BRL) Lines: 30 Xref: watmath net.unix:7547 net.lang.c:8410 In article <1740@ittatc.ATC.ITT.UUCP> yoda@ittatc.ATC.ITT.UUCP (Todd C. Williams [Jedi Knight]) writes: >Is it possible to use gets() after scanf() ? >Is it a good idea to try to use gets() after scanf() ? >It doesn't work, so I tried adding "fflush(stdin);" >just before the gets(), but that doesn't help either. >If I do 2 "gets(str)"'s in a row, the second one works. > >Apparently, the scanf() is reading UP TO the newline, but >leaving that newline in the buffer; the gets() sees the >newline and thinks it is finished. scanf() does whatever you tell it to with the input stream then stops. If you make it eat newlines, it will. There should be no problem mixing any of the STDIO input methods. However, it sounds to me like your application would be programmed better with the following scheme: while get-a-line-using-fgets parse-line-with-sscanf By the way, don't use gets(); your input buffer can be overrun if an input line is very long, resulting in unpredictable malfunctioning of your application. When you use fgets(), it is often wise to test the last character in the record and if it is a newline, replace it with a 0 string terminator before parsing the record. Since this advice is independent of UNIX, I'm cross-posting to net.lang.c.