[英]read tfrom terminal using system calls and output last 6 lines of the input
[英]Mechanism for linux output in terminal when using system function read() & write()
碼:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char buf[BUFSIZ];
int n;
while((n = read(0, buf, BUFSIZ)) > 0 && printf("1:%d ", n))
{
printf("2:%d ", n);
write(1, buf, n);
}
return 0;
}
pupu(my input)
pupu(output)
popopo(my input)
popopo(output)
1:5 2:5 1:7 2:7(output)
我的問題:如何運作?
(為什么在n_read之前緩沖文本輸出?)
標准I / O函數(如printf
)被緩沖 ,這意味着直到其緩沖區已滿或顯式刷新后,才輸出到stdout
。
另一方面,直接寫入輸出文件描述符的操作不會緩沖,而是直接寫入。
您所擁有的是將直接輸出和緩沖輸出混合在一起,直到程序退出並且刷新緩沖區后,緩沖輸出才真正寫入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.