C中perror函数两个输出的执行顺序的区别

问题描述

Difference between output sequence on the same code on removing "\n"

#include<stdio.h>
#include<errno.h>
#include<string.h>


int main()
{
FILE * fp;
fp = fopen("GeeksForGeeks","/root/C");
printf("Value of error no : %d\n",errno);
printf("The error message is : %s\n",strerror(errno));
perror("Message from perror.");
return 0;
}

这是我的代码,但是当我删除“\n”时,perror 的执行顺序发生了变化。 谁能解释一下原因?

解决方法

很可能,stdout 处于行缓冲模式,因此它在 \n 上刷新,而 perror 写入 stderr

fflush(stdout); 调用之前添加 perror 将修复它。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...