-
errno()
定义在errno.h中,#include <errno.h>
记录系统最后一次的错误码,int类型
每个errno值对应一个字符串表示的错误类型,可以使用strerror(errno)获取errno对应的错误
参照https://blog.csdn.net/qq_15437629/article/details/108123190int main(int argc, char const *argv[]) { int fw, fr; fw = open("1.txt", O_WRONLY); if(fw < 0) { printf("%d\n", errno); printf("%s\n", strerror(errno)); } return 0; }
hohj@ubuntu20-10:~/Documents/C/sys$ ./sysio 2 No such file or directory
-
perror()
定义在stdio.h,#include <stdio.h>
用来将上一个函数发生错误的原因输出到标准设备(stderr)
此错误原因依照全局变量errno 的值来决定要输出的字符串。int main(int argc, char const *argv[]) { int fw, fr; fw = open("1.txt", O_WRONLY); if(fw < 0) { perror("open()"); exit(1); } return 0; }
hohj@ubuntu20-10:~/Documents/C/sys$ ./sysio open(): No such file or directory
linuxC异常处理
最新推荐文章于 2022-11-15 17:30:42 发布