c - 跳过 read 语句到 printf 语句

标签 c

<分区>

为什么它在从套接字 sockfd 读取之前打印 hello 然后 good luck 然后又打印 hello ?
那为什么它跳过阅读打印?

int number=read(sockfd,&buff,500);
while(number>0)
{
printf("hello ");
number=read(sockfd,&buff,500);
printf("good luck");
}

最佳答案

因为当连接到终端时,stdout 是行缓冲的,在打印内容的末尾添加一个 \nprinf("hello\n "); 或使用 fflush:

while(number>0)
{
    printf("hello ");
    fflush(stdout);
    number=read(sockfd,&buff,500);
    printf("good luck");
    fflush(stdout);
}

关于c - 跳过 read 语句到 printf 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196051/

相关文章:

c - 不确定C编程的三个简单函数

c++ - 我可以使用 c/c++ 与硬件设备通信吗?

C 中的倒数计时器不卡住用户输入

c - 我应该如何初始化 pthread 互斥量?

c - OpenMP 未显示正确的线程号 - C

c - 在没有 lseek() 的情况下写入和读取 linux/proc/... 文件系统

c - 链接器提示 C 代码中的函数有多个定义,而实际上只有一个函数

c - <search.h> 头文件不可用

c - 将文件中的数据读入C中的结构

c - 为什么我需要 stat 来检查目录是否已经存在?