linux下使用errno的幾個需要主要的地方

來源:互聯網
上載者:User

以下內容轉自http://hi.baidu.com/tekuba/item/b247ce5a9a8653c8d2e10c51

 

errno - number of last error

errno 記錄linux系統的最後一次錯誤碼。代碼是一個int型的值,在errno.h中定義
注意引用他的方法,看下面一例:
if (somecall() == -1)
{
  printf("somecall() failed\n");
if(errno == ...)
{ ... }
}
這樣的例子並不能總是得到somecall這個函數的運行所產生的錯誤碼,因為很可能是printf這個函數產生的。
再看下面一例:
if (somecall() == -1)
{
  int errsv = errno;/*首先將errno的值儲存*/
  printf("somecall() failed\n");
  if (errsv == ...) /*用這個儲存的值判斷*/
{ ... }
}
另外需要特別注意:
只有當一個庫函數失敗時,errno才會被設定。當函數成功運行時,errno的值不會被修改。這意味著我們不能通過測試errno的值來判斷是否有錯誤存在。反之,只有當被調用的函數提示有錯誤發生時檢查errno的值才有意義。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.