fflush(stdin); ??



  • Der Final Draft sagt zu fflush() folgendes:

    7.19.5.2 The fflush function
    Synopsis

    #include <stdio.h>
    int fflush(FILE *stream);
    Description

    If stream points to an output stream or an update stream in which the most recent
    operation was not input
    , the fflush function causes any unwritten data for that stream
    to be delivered to the host environment to be written to the file; otherwise, the behavior is
    undefined.

    If stream is a null pointer, the fflush function performs this flushing action on all
    streams for which the behavior is defined above.
    Returns

    The fflush function sets the error indicator for the stream and returns EOF if a write
    error occurs, otherwise it returns zero.

    Daraus folgt, das fflush(stdin) ein undefiniertes verhalten an den tag legt.

    Und wir wissen ja alles was undefiniert heisst -> zusammenbruch der kausalitaet 😃 oder aehnliches 😉



  • und weil hier keiner ne "alternative" postet, mach ich das mal:

    folgendes killt den rest einer eventuell noch im puffer liegenden "zeile", also alles bis zu einem eventuell vorkommenden '\n':

    int c; // int c, nicht char c.
    while ((c = getchar()) != EOF && c != '\n'); // kein {} block, stimmt so
    

Anmelden zum Antworten