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\n", strerror( errno ) );
26       printf( _strerror( "_strerror says open failed" ) );
27    }
28    else
29    {
30       printf( "open succeeded on input file\n" );
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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM