Flushing stdout output stream in C
stdout
This article will demonstrate various methods on how to flush an output stream
in C language .
Use function in C language fflush
to refresh stdout
output stream
I/O
The C standard library
provides a stdio
buffered version of the I/O operations that are performed in user space, which improves performance for common use cases. Generally speaking, accessing files and operating on files is provided by operating system services, so users eventually need to make system calls, such as opening a file. Frequent system calls can slow down the program because it requires accessing the operating system's kernel data structures and transferring control back and forth. Therefore, stdio
the C library maintains some buffers to handle input/output operations when using the function calls.
If the user needs to force a write to the kernel buffer, then fflush
the flush is required for the given stream provided by the _flush_flush_function. _flush_flush_flush_function fflush
takes a FILE
single argument of _flush_flush_flush_function, a pointer to the given stream. Note that fflush
_flush_flush_function forces a write to the output stream, while discarding any buffered data for the input stream (there is a seekable file). If the argument is _flush_flush_flush_function NULL
, it will flush all open output streams.
Note that fflush
there is no guarantee that the written data is physically stored, as this requires the kernel buffer to be flushed (which can be fsync
done using the call, see here ).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
int main(int argc, char *argv[]) {
char *username;
size_t len;
int lnmax = 256;
username = malloc(lnmax);
if (username == NULL)
perror("malloc");
printf("Username: ");
fflush(stdout);
if (fgets(username, lnmax, stdin) == NULL)
exit(EXIT_FAILURE);
printf("Your username is set to - %s", username);
exit(EXIT_SUCCESS);
}
Output:
Username: tmp
Your username is set to - tmp
printf
Use functions
in C to demonstrate fflush
behavior
Note that some streams (such as stderr
) are unbuffered. In contrast, the implicit write function that writes to stdout
the stream printf
is buffered, and if we execute an infinite loop below that prints one character per iteration, nothing will be written to the stream until the internal buffer is full. Therefore, the result of the code example below is a burst of printed bullet
characters. Note that we call the function on each iteration usleep
to slow down the execution speed for the human eye so that it can be clearly observed.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
while (1) {
printf(".");
usleep(1e3);
}
exit(EXIT_SUCCESS);
}
Alternatively, if we fprintf
replace printf
the call to print to stderr
the stream with , we will get iterative printing of one character per second. Again, the 1 second delay between iterations is just for good demonstration purposes.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
while (1) {
fprintf(stderr, ".");
sleep(1);
}
exit(EXIT_SUCCESS);
}
Finally, if we need stdout
to emulate the same behavior seen in the previous example code on a stream, we can printf
add fflush
a call after the function. This will force the C library buffer to be written to the kernel buffer on each iteration, resulting in similar behavior.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
while (1) {
printf(".");
fflush(stdout);
sleep(1);
}
exit(EXIT_SUCCESS);
}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
在C中将整数转换为字符
Publish Date:2024/01/03 Views:135 Category:C语言
-
本教程介绍了在C中将整数转换为字符的不同方法。在C编程语言中,将整数转换为字符在各种情况下都很重要。在C中,字符是以ASCII值表示的,因此转换过程相对简单。
在 C 语言中使用 typedef enum
Publish Date:2023/05/07 Views:372 Category:C语言
-
本文介绍了如何在 C 语言中使用 typedef enum。使用 enum 在 C 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。
C 语言中的静态变量
Publish Date:2023/05/07 Views:170 Category:C语言
-
本文介绍了如何在 C 语言中使用静态变量。在 C 语言中使用 static 变量在函数调用之间保存变量值
C 语言中生成随机数
Publish Date:2023/05/07 Views:159 Category:C语言
-
本文演示了如何在 C 语言中生成随机数。使用 rand 和 srand 函数在 C 语言中生成随机数
C 语言中的 i++ 与++i
Publish Date:2023/05/07 Views:130 Category:C语言
-
本文演示了如何在 C 语言中使用前缀增量与后缀增量运算符。C 语言中++i 和 i++ 记号的主要区别
C 语言中获取当前工作目录
Publish Date:2023/05/07 Views:280 Category:C语言
-
本文演示了如何在 C 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法
C 语言中的排序函数
Publish Date:2023/05/07 Views:212 Category:C语言
-
本文演示了如何在 C 语言中使用标准库排序函数。使用 qsort 函数对 C 语言中的整数数组进行排序
C 语言中的 extern 关键字
Publish Date:2023/05/07 Views:132 Category:C语言
-
本文介绍了如何在 C 语言中使用 extern 关键字。C 语言中使用 extern 关键字来声明一个在其他文件中定义的变量