strerror 函数

收藏
75
23

strerror编辑

本词条缺乏 名片图,补充相关内容使词条更完整,还能快速升级,赶忙来 编辑吧!
经过标准错误的标号,得到错误的描述字符串 ,将单纯的错误标号转为字符串描述,方便用户查找错误。
外文名
strerror
语    言
C语言
参    数
错误标号(即error)
返回值
描述字符串(char *)

目录

1函数名node

2函数做用linux

3头文件编程

4函数原型api

5举例dom

1函数名编辑

strerror, _strerror, _wcserror, __wcserror

2函数做用编辑

Get a system error message (strerror, _wcserror) or prints a user-supplied error message (_strerror, __wcserror).
获取系统 错误信息或打印用户 程序错误信息。

3头文件编辑

#include <string.h>

4函数原型编辑

1
2
3
4
char * strerror (interrnum);
char *_strerror(constchar*strErrMsg);
wchar_t *_wcserror(interrnum);
wchar_t *__wcserror(constwchar_t*strErrMsg)
参数:
errnum:错误标号,一般用errno(标准错误号,定义在errno.h中)
Error number.
strErrMsg
User-supplied message.
返回:
指向 错误信息指针(即:错误的描述字符串)。

5举例编辑

例一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
intmain( void )
{
FILE *fp;
externinterrno;
char *message;
if (NULL==(fp= fopen ( "/dev/dsp" , "r+" )))
{
printf ( "errno=%d\n" , errno );
message= strerror ( errno );
printf ( "Mesg:%s\n" ,message);
}
exit (0);
}
输出:
error=2
Mesg:No such file or direcory
例二:
// crt_perror.c
// compile with: /W1
/* This program attempts to open a file named
* NOSUCHF.ILE. Because this file probably doesn't exist,
* an error message is displayed. The same message is
* created using perror, strerror, and _strerror.
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <share.h>
int main( void )
{
int fh;
if( _sopen_s( &fh, "NOSUCHF.ILE", _O_RDONLY, _SH_DENYNO, 0 ) != 0 )
{
/* Three ways to create error message: */
perror( "perror says open failed" );
printf( "strerror says open failed: %s\n",
strerror( errno ) ); // C4996
printf( _strerror( "_strerror says open failed" ) ); // C4996
// Note: strerror and _strerror are deprecated; consider
// using strerror_s and _strerror_s instead.
}
else
{
printf( "open succeeded on input file\n" );
_close( fh );
}
}
输出:
perror says open failed: No such file or directory
strerror says open failed: No such file or directory
_strerror says open failed: No such file or directory
 
转自 : http://baike.baidu.com/link?url=pllZsYxUR2EJ26CRuWn_F8x7cUimLjEi6g_tPsEnENMv3L4zzXhwqtQ1u7ry8IrHbCh29BbgJTVACTPfawZ1J_
 
 

linux下错误的捕获:errno和strerror的使用

 
转自 http://www.douban.com/note/165931644/
相关文章
相关标签/搜索