How does Fflush work C?


In the C Programming Language, the fflush function writes any unwritten data in stream’s buffer. If stream is a null pointer, the fflush function will flush all streams with unwritten data in the buffer.

Linux fsync Function

fsync(): fsync() transfers ("flushes") all modified in-core data of the file referred to by the file descriptor fd to the disk device so that all changed information can be retrieved even if the system crashes or is rebooted.

Functions for File Handling

rewind(): rewind() takes the file pointer to the beginning of the file.

randomize(): The randomize() function initializes the random number generator with a random value based on time.

Alternative to fflush in C

What can we use instead of fflush in C?
Three possible answers:

  1. Quit using scanf.
  2. Use fgets and the sscanf.
  3. Use this to eat the newline:
    while((c = getchar()) != 'n' && c != EOF) /* discard the character */

Understanding Standard Input

Why is stdin used?
Standard input (stdin) is used for the read operation in programs requiring data transfers. Not all programs necessarily need input streams. For example, dir and ls programs operate without stream data input.

Lascia un commento