linux错误处理
来源:互联网 发布:mac上做u盘启动盘 编辑:程序博客网 时间:2024/05/18 01:06
函数或者系统调用在失败时会设置外部变量errno的值来指明失败原因(错误码保存在errno中)。并且必须在函数报告出错之后,立即检查errno变量,因为它可能被下一个函数调用所覆盖,即使下一个函数自身并没有出错,也会覆盖这个变量。错误代码(errnum)的取值和含义在errno.h中,可以在man手册查找。
有三个有用的函数可以用来报告出现的错误,strerror ,perror ,printf(%m):
#include <string.h>
char* strerror(int errnum);
把错误代码映射为一个对发生的错误进行说明的字符串。
#include <stdio.h>
void perror(const char* s);
把errno变量中报告的当前错误映射到一个字符串,并把它输出到标准出错流。该字符串的前面加上字符串s(如果不为空)中给出的信息,在加上一个冒号和一个空格。
#include <stdio.h>
int printf("%m");打印当前错误信息字符串。
示例程序:
error.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
FILE* fp = fopen("hello", "r");
if(NULL == fp)
{//失败,会设置错误码
printf("文件打开失败!\n");
printf("errno = %d\n", errno);
printf("%s\n", strerror(errno));
perror("当前错误信息");
printf("errno string:[%m]\n");
}
return 0;
}
注意:在程序中用到errno变量时,不要忘记加errno.h头文件。
0 0
- linux 错误处理小结
- linux c 错误处理
- linux错误处理
- linux下asm错误处理
- Linux C语言错误处理
- Linux C语言错误处理
- linux errno错误处理函数
- linux 错误处理类函数
- linux中段错误的处理
- Linux编程错误处理函数
- 安装Linux错误问题处理
- linux C++之错误处理
- linux中段错误的处理
- Linux - 错误处理 代码(C)
- Linux编程错误处理函数
- Linux下各种错误处理
- Linux错误处理与错误号
- linux错误检测及处理办法
- XMPP的IOS聊天客户端程序
- spring MVC 转发与重定向(传参)
- C++流的基本概念
- 时隔两年,我又捡起了博客
- iOS:适配(四)--不同版本api适配做(sdk)
- linux错误处理
- UVa 11605 - Lights inside a 3d Grid 期望
- DedeCMS程序include目录文件作用解析
- 第七周项目七“撞错”体验
- android 将应用程序从手机内存移动到SD卡
- android mmap的使用
- ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP
- MySql数据导出到cvs文件命令
- Use Local Or Global Index?