如果看到這個(gè)標(biāo)題時(shí),還不知道什么是errno,那么,你就和我一樣.呵呵,我編程也有4,5年了,今天才知道errno.
errno不是我定義的一個(gè)變量,也不是否個(gè)隨意的變量名,而是crt庫(kù)中定義的一個(gè)全局變量
定義:errno Constants (
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_errno_Constants.asp)
#if?????(defined(_MT)?||?defined(_DLL))?&&?!defined(_MAC)
_CRTIMP?extern?int?*?__cdecl?_errno(void);
#define?errno???(*_errno())
#else???/*?ndef?_MT?&&?ndef?_DLL?*/
_CRTIMP?extern?int?errno;
#endif??/*?_MT?||?_DLL?*/
實(shí)際上,就是再一些crt函數(shù)調(diào)用后,errno會(huì)被賦值,表示函數(shù)調(diào)用的狀態(tài).有點(diǎn)類似window api中的GetLastError
這些crt函數(shù)包括fgetpos或者ftell和strtol之類.
在什么時(shí)候用它呢?看下面的一個(gè)例子:
????const char*?chTest?=?"123456789222299999999";
????char*?pStop?=?NULL;
????int?nValue?=?strtol(chTest,?&pStop,?10);
????int?nError?=?errno;
????if(nError?==?ERANGE)
????????perror(chTest);
這個(gè)時(shí)候,nValue = 0x7fffffff,你如果只是根據(jù)它來(lái)判斷,是不知道實(shí)際上已經(jīng)出錯(cuò)了.所以,這個(gè)時(shí)候必須借助errno來(lái)判斷狀態(tài).
慚愧啊,今天才知道.今天看strtol的msdn文檔,看到一句話For both functions,
errno is set to
ERANGE if overflow or underflow occurs.,我看了半天沒(méi)有找到errno,后來(lái)一搜,才發(fā)現(xiàn)它居然是個(gè)全局變量.