delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/11/24/15:49:06

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-MimeOLE: Produced By Microsoft Exchange V6.0.6318.0
MIME-Version: 1.0
Subject: RE: Flushing stdin (was: Re: gcc problem?)
Date: Sun, 24 Nov 2002 12:48:50 -0800
Message-ID: <BF653A78FF4ED0468924C8721AC5EAE701924F90@df-muttley.dogfood>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
From: "Stephan Mueller" <smueller AT Exchange DOT Microsoft DOT com>
To: "Eric R. Krause" <ekraus02 AT baker DOT edu>, <carlo AT astra DOT ph>
Cc: <cygwin AT cygwin DOT com>
X-OriginalArrivalTime: 24 Nov 2002 20:48:50.0979 (UTC) FILETIME=[E4D0C730:01C293FA]
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id gAOKn5G16840

I believe the name of the company you're referring to is Microsoft.  (In
addition to the spelling, note that the 's' is not capitalized.)  It
matters, because you'll have a much easier time reporting the small bug
you've discovered (these do occur in large systems, and the
documentation for large systems) if you use the correct company name
when searching for a web site on which to actually submit the bug
report.

BTW, which C standard do you mean?  7.9.5.2 doesn't seem to exist in
either of my copies (I have the 1989 ANSI version, and the 1999 ISO
version handy).

stephan(speaking, of course, only for myself, and not my employer);

-----Original Message-----
From: Eric R. Krause [mailto:ekraus02 AT baker DOT edu] 
Sent: Sunday, November 24, 2002 12:14 PM
To: carlo AT astra DOT ph
Cc: cygwin AT cygwin DOT com
Subject: Flushing stdin (was: Re: gcc problem?)


Carlo,

Visual C++ 6.0 CRT (and AFAICT, that of Visual C++.NET too) allow you to
flush an input stream.  The only problem with that is that the C
standard apparently defines flushing ONLY for output streams (sec.
7.9.5.2).  Why in the hell MicroSquash didn't disclose that this
behavior was M$-specific, who knows--it's yet another way they try to
lock you into their software.

For reading words entered by the user, I'd approach the situation using
fgets() and a pair of string buffers--one to hold the input line and one
to hold the word that is sscanf()'ed.  After we've read the word, we can
loop-read until there are no more characters on stdin (in case we
entered past the size of the string buffer), knowing that our word is in
a separate buffer and that each iteration both are NULLed out.

Here's the code...

#include <string.h>
#include <stdio.h>
int main() {
    char string[80];
    char word[80];  /* extra string buffer */
    int i;

    for (i = 0; i < 2; i++) {
        memset(string, 0, 80 * sizeof(char));
        memset(word, 0, 80 * sizeof(char));
        printf("Enter some words: ");
        fgets(string, 80, stdin);   /* see note A */
        sscanf(string, "%s", word);
        printf("The first word you entered was... %s\n", word);
        while (!strchr(string, '\n'))
            fgets(string, 80, stdin);
    }
    return 0;
}

Note A:
Pressing Enter as soon as the prompt comes up will cause fgets() to
write a newline and a NULL to the buffer and return.  If you want to
FORCE the user to enter a non-blank line, then change
    fgets(string, 80, stdin);
to
    do {
        fgets(string, 80, stdin);
    } while (string[0] == '\n');

---
Eric R. Krause


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019