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/
相關文章
相關標籤/搜索