1FFLUSH(3P) POSIX Programmer's Manual FFLUSH(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 fflush — flush a stream
13
15 #include <stdio.h>
16
17 int fflush(FILE *stream);
18
20 The functionality described on this reference page is aligned with the
21 ISO C standard. Any conflict between the requirements described here
22 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
23 defers to the ISO C standard.
24
25 If stream points to an output stream or an update stream in which the
26 most recent operation was not input, fflush() shall cause any unwritten
27 data for that stream to be written to the file, and the last data modi‐
28 fication and last file status change timestamps of the underlying file
29 shall be marked for update.
30
31 For a stream open for reading with an underlying file description, if
32 the file is not already at EOF, and the file is one capable of seeking,
33 the file offset of the underlying open file description shall be set to
34 the file position of the stream, and any characters pushed back onto
35 the stream by ungetc() or ungetwc() that have not subsequently been
36 read from the stream shall be discarded (without further changing the
37 file offset).
38
39 If stream is a null pointer, fflush() shall perform this flushing
40 action on all streams for which the behavior is defined above.
41
43 Upon successful completion, fflush() shall return 0; otherwise, it
44 shall set the error indicator for the stream, return EOF, and set errno
45 to indicate the error.
46
48 The fflush() function shall fail if:
49
50 EAGAIN The O_NONBLOCK flag is set for the file descriptor underlying
51 stream and the thread would be delayed in the write operation.
52
53 EBADF The file descriptor underlying stream is not valid.
54
55 EFBIG An attempt was made to write a file that exceeds the maximum
56 file size.
57
58 EFBIG An attempt was made to write a file that exceeds the file size
59 limit of the process.
60
61 EFBIG The file is a regular file and an attempt was made to write at
62 or beyond the offset maximum associated with the corresponding
63 stream.
64
65 EINTR The fflush() function was interrupted by a signal.
66
67 EIO The process is a member of a background process group attempting
68 to write to its controlling terminal, TOSTOP is set, the calling
69 thread is not blocking SIGTTOU, the process is not ignoring
70 SIGTTOU, and the process group of the process is orphaned. This
71 error may also be returned under implementation-defined condi‐
72 tions.
73
74 ENOMEM The underlying stream was created by open_memstream() or
75 open_wmemstream() and insufficient memory is available.
76
77 ENOSPC There was no free space remaining on the device containing the
78 file or in the buffer used by the fmemopen() function.
79
80 EPIPE An attempt is made to write to a pipe or FIFO that is not open
81 for reading by any process. A SIGPIPE signal shall also be sent
82 to the thread.
83
84 The fflush() function may fail if:
85
86 ENXIO A request was made of a nonexistent device, or the request was
87 outside the capabilities of the device.
88
89 The following sections are informative.
90
92 Sending Prompts to Standard Output
93 The following example uses printf() calls to print a series of prompts
94 for information the user must enter from standard input. The fflush()
95 calls force the output to standard output. The fflush() function is
96 used because standard output is usually buffered and the prompt may not
97 immediately be printed on the output or terminal. The getline() func‐
98 tion calls read strings from standard input and place the results in
99 variables, for use later in the program.
100
101
102 char *user;
103 char *oldpasswd;
104 char *newpasswd;
105 ssize_t llen;
106 size_t blen;
107 struct termios term;
108 tcflag_t saveflag;
109
110 printf("User name: ");
111 fflush(stdout);
112 blen = 0;
113 llen = getline(&user, &blen, stdin);
114 user[llen-1] = 0;
115 tcgetattr(fileno(stdin), &term);
116 saveflag = term.c_lflag;
117 term.c_lflag &= ~ECHO;
118 tcsetattr(fileno(stdin), TCSANOW, &term);
119 printf("Old password: ");
120 fflush(stdout);
121 blen = 0;
122 llen = getline(&oldpasswd, &blen, stdin);
123 oldpasswd[llen-1] = 0;
124
125 printf("\nNew password: ");
126 fflush(stdout);
127 blen = 0;
128 llen = getline(&newpasswd, &blen, stdin);
129 newpasswd[llen-1] = 0;
130 term.c_lflag = saveflag;
131 tcsetattr(fileno(stdin), TCSANOW, &term);
132 free(user);
133 free(oldpasswd);
134 free(newpasswd);
135
137 None.
138
140 Data buffered by the system may make determining the validity of the
141 position of the current file descriptor impractical. Thus, enforcing
142 the repositioning of the file descriptor after fflush() on streams open
143 for read() is not mandated by POSIX.1‐2008.
144
146 None.
147
149 Section 2.5, Standard I/O Streams, fmemopen(), getrlimit(), open_mem‐
150 stream(), ulimit()
151
152 The Base Definitions volume of POSIX.1‐2017, <stdio.h>
153
155 Portions of this text are reprinted and reproduced in electronic form
156 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
157 table Operating System Interface (POSIX), The Open Group Base Specifi‐
158 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
159 Electrical and Electronics Engineers, Inc and The Open Group. In the
160 event of any discrepancy between this version and the original IEEE and
161 The Open Group Standard, the original IEEE and The Open Group Standard
162 is the referee document. The original Standard can be obtained online
163 at http://www.opengroup.org/unix/online.html .
164
165 Any typographical or formatting errors that appear in this page are
166 most likely to have been introduced during the conversion of the source
167 files to man page format. To report such errors, see https://www.ker‐
168 nel.org/doc/man-pages/reporting_bugs.html .
169
170
171
172IEEE/The Open Group 2017 FFLUSH(3P)