From: CSBVAX::MRGATE!@KL.SRI.Com:R022DB3L@VB.CC.CMU.EDU@SMTP 18-SEP-1987 22:07 To: EVERHART Subj: Re: 2 C Questions Received: from VB.CC.CMU.EDU by KL.SRI.COM with TCP; Thu 17 Sep 87 21:06:38-PDT Received: from Mailer by VB.CC.CMU.EDU; Fri, 18 Sep 87 00:09 EDT Date: Thu, 18 Sep 87 00:09:35 EDT From: Mithrandir Subject: Re: 2 C Questions To: info-vax@KL.SRI.COM For Richard Steinberger's query: > I wrote a short main routine (see below) to test a function. One > of the inputs is an integer number, and the next input is a filename. > The problem is that the scanf() that reads the number apparently leaves > a LF character in a buffer that is then read by the code that is > expecting the filename. ..... > Why does scanf leave a LF > character, or am I misinterpreting what's going on? Are there solutions > other than using a getchar() call after a scanf() that preceeds a gets() > or getchar() call? .... I've never actually programmed in Vax C, but just from my general C work, I would expect this to be the case, according to the definition of SCANF. Basically, your call to scanf is causing an I/O request, to which you enter the string size and press RETURN. Thus, your input buffer contains the number followed by a newline. You're asking scanf to read a decimal input field into an integer variable. According to scanf (or at least the definition of it that I have handy), an "input field" is: * All characters up to **(but not including)** the next whitespace character * All characters up to the first one that cannot be converted under the current format specification (such as an 8 or 9 under octal format) * Up to 'n' characters where 'n' is the specified field width. "Whitespace" is defined as one of the characters blank ( ), tab (\t) or **newline (\n)**. Therefore, when you do a scanf("%d",&variable), scanf simply parses through the input until it hits the whitespace character (the newline). The number it reads gets assigned into the variable, and according to the first rule above, the whitespace character is not parsed, and therefore stays in the input buffer for the next call to scanf (or any function accessing the standard input stream). As far as a solution, how about just changing your scanf to: scanf("%d\n",&variable) (Actually, according to scanf, if there is any whitespace in the format string, it will scan over as much whitespace as necessary in the input at that point, so I suppose technically, you could use a space or \t in place of the \n above since they're all whitespace, but the \n seems logical). I do presume however, that even the above code would run into problems if the user were to input a string like "10 20{RETURN}", since the scanf would stop at the space, and you'd still have the rest of the string in the buffer for the next scanf. Perhaps an even better solution would be: scanf("%d%*[^\n]\n",&variable) What this code will do is read an integer, followed by a string which will be ignored (due to the *), the string consisting of the series of characters in the input which aren't a newline. The [] construct is a character search set specifier - since the first character is ^, it's a inverted set, matching any characters not listed. Effectively, this will skip over any extra characters on the input up to the newline, and then the \n in the format string will read the newline out of the buffer. Please note that I haven't actually had the time to try out these function calls, but I used function calls very similar this summer, so if they aren't quite perfect, play around a bit with the concept... :-) -- David Bolen Arpanet: R022DB3L@VB.CC.CMU.EDU Carnegie-Mellon University Bitnet : R022DB3L@CMCCVB Pittsburgh, PA 15213