c语言中的errno

越来越感觉到c语言的短小精悍。

有一次要调试一个代码,直接从ftp上下载下来,打开vs就开始调试。无论怎么调试就是不对,单步运行定位错误到一个打开文件的函数。原来是文件没有打开。但是没有具体的错误信息,调试一下子陷入了僵局。记得以前用MFC的时候里面有个函数叫做GetLastError(),可以获取最后出错的原因,如果c语言有就好了。上网搜索了一下果然有这个东西,就叫做errno。errno是定义"error.h"里的错误码,“error.h”定义了一系列的宏来表示相应的错误信息,通过检查errono再在“erro.h”里面查找就能找到相应的出错信息。


from  wikipedia:

errno.h 是C语言C标准库里的头文件,定义了通过错误码来回报错误信息的宏:

  • errno宏定义为一个int型态的左值, 包含任何函数使用errno功能所产生的上一个错误码。
  • 一些表示错误码,定义为整数值的宏:
  • EDOM 源自于函数的参数超出范围,例如sqrt(-1)
  • ERANGE 源自于函数的结果超出范围,例如strtol("0xfffffffff",NULL,0)
  • EILSEQ 源自于不合法的字符顺序,例如wcstombs(str, L"xffff", 2)

但是一个一个手工查找毕竟麻烦,还好标准库为我们提供了一些函数。

 1 void perror( const char *string );//Print an error message.<stdio.h> or <stdlib.h>
 2 //msdn 示例代码
 3 /* PERROR.C: This program attempts to open a file named
 4  * NOSUCHF.ILE. Because this file probably doesn't exist,
 5  * an error message is displayed. The same message is
 6  * created using perror, strerror, and _strerror.
 7  */
 8 
 9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <io.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 
17 void main( void )
18 {
19    int  fh;
20 
21    if( (fh = _open( "NOSUCHF.ILE", _O_RDONLY )) == -1 )
22    {
23       /* Three ways to create error message: */
24       perror( "perror says open failed" );
25       printf( "strerror says open failed: %s
", strerror( errno ) );
26       printf( _strerror( "_strerror says open failed" ) );
27    }
28    else
29    {
30       printf( "open succeeded on input file
" );
31       _close( fh );
32    }
33 }
34 
35 
36 /*Output
37 
38 perror says open failed: No such file or directory
39 
40 strerror says open failed: No such file or directory
41 _strerror says open failed: No such file or directory*/

上述例子中也出现了另外一个打印错误信息的函数即——strerror

/*strerror, _strerror
Get a system error message (strerror) or prints a user-supplied error message (_strerror).include "string.h"
strerror and _strerror return a pointer to the error-message string. Subsequent calls to strerror or _strerror can overwrite the string.

*/
char *strerror( int errnum );
char *_strerror( const char *strErrMsg );

参考网站:

http://zh.wikipedia.org/wiki/Errno.h

http://blog.csdn.net/zljjava/article/details/7767183 介绍了linux下的一些errorno的打印方法,printf("%m", errno);好像在windows下不行

http://www.cnblogs.com/yaohj/archive/2011/01/28/1946817.html介绍了一些具体使用的细节

 http://361324767.blog.163.com/blog/static/114902525201251395554338/   perror()的一些注意事项

附录:MSDN 上关于 GetLastError()用法的一个例子:

  

 1 /*GetLastError
 2 This function returns the calling thread’s last-error code value. A remote application interface (RAPI) version of this function exists, and it is named CeGetLastError. */
 3 
 4 DWORD GetLastError( void );
 5 
 6 /*FormatMessage
 7 This function formats a message string. */
 8 
 9 DWORD FormatMessage (
10 DWORD dwFlags, 
11 LPCVOID lpSource, 
12 DWORD dwMessageId, 
13 DWORD dwLanguageId, 
14 LPTSTR lpBuffer, 
15 DWORD nSize, 
16 va_list *Arguments); 
17 
18 
19 //示例用法
20 LPVOID lpMsgBuf;
21 FormatMessage( 
22     FORMAT_MESSAGE_ALLOCATE_BUFFER | 
23     FORMAT_MESSAGE_FROM_SYSTEM | 
24     FORMAT_MESSAGE_IGNORE_INSERTS,
25     NULL,
26     GetLastError(),
27     0, // Default language
28     (LPTSTR) &lpMsgBuf,
29     0,
30     NULL 
31 );
32 // Process any inserts in lpMsgBuf.
33 // ...
34 // Display the string.
35 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
36 // Free the buffer.
37 LocalFree( lpMsgBuf );

2014-05-08  07:56:29

原文地址:https://www.cnblogs.com/pasion-forever/p/3715330.html