perror和printf的区别
我们大多都使用printf来打印一些信息,其它的接口都比较少用。perror函数用的比较少,但是用起来调试比printf打印更加详细
注意:perror不能带参数,printf可以带参数
printf例子:
#include<stdio.h>
int main(void)
{
FILE *fp;
fp = fopen("/home/book/test_file", "r+");
if (NULL == fp)
{
printf("fopen error!\n");
}
return 0;
}
输出结果:
fopen error!
perror例子:
#include<stdio.h>
#include <errno.h>
#include <string.h>
int main(void)
{
FILE *fp;
fp = fopen("/home/book/test_file","r+");
if (NULL == fp)
{
perror("fopen error");
}
return 0;
}
输出结果:
fopen error: No such file or directory
相比之下,perror会打印更详细的错误log信息