﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-continue123456-随笔分类-c</title><link>http://www.cppblog.com/continue123456/category/20450.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jul 2013 16:57:25 GMT</lastBuildDate><pubDate>Tue, 09 Jul 2013 16:57:25 GMT</pubDate><ttl>60</ttl><item><title>关于errno</title><link>http://www.cppblog.com/continue123456/archive/2013/07/06/201570.html</link><dc:creator>烽火依然</dc:creator><author>烽火依然</author><pubDate>Sat, 06 Jul 2013 11:21:00 GMT</pubDate><guid>http://www.cppblog.com/continue123456/archive/2013/07/06/201570.html</guid><wfw:comment>http://www.cppblog.com/continue123456/comments/201570.html</wfw:comment><comments>http://www.cppblog.com/continue123456/archive/2013/07/06/201570.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/continue123456/comments/commentRss/201570.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/continue123456/services/trackbacks/201570.html</trackback:ping><description><![CDATA[<span style="color: red;"><div><span style="color: #000000; font-family: arial, 宋体, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: auto; text-align: start; text-indent: 30px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none;">errno是在errno.h中定义的一个整型变量，<br />当且仅当库函数执行失败时，errno被设置，<br />errno中保存错误代码( int型的值 )，具体错误信息可以调用perror()或strerror()输出。<br />errno 记录系统的最后一次</span><span style="color: #000000;">错误代码</span><span style="color: #000000; font-family: arial, 宋体, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 24px; orphans: auto; text-align: start; text-indent: 30px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none;">。<br /><br /></span></div>注意：errno是在已经知道出错的情况下，表示出错类型的；最好不要拿它来作为判断是否出错的依据。</span><br /><br /><strong>正确用法：</strong><br />1.如果函数本身返回一个指示调用成功与否的值，则先用这个值判断是否失败，如果确定失败，在根据errno判断失败类型。&nbsp;例：<br /><br /><p>&nbsp;</p><div>res = func();<br />if( res == 某个失败值 )<br />{<br />&nbsp;&nbsp; printf("errno = %d\n",errno );<br />}</div><p>&nbsp;</p><p>2.调用函数前先手动设置errno = 0。例：</p><p><br /></p>errno = 0;<br />func();<br />//这个时候errno就能说明问题了。<br />if( 0 == errno )<br />&nbsp;&nbsp; printf( "succeed" );<br />else<br />&nbsp;&nbsp; printf( "failed" ); <br /><br /><br /><strong>错误用法</strong>：<br />func();<br />if( 0 != errno )<br />&nbsp;&nbsp; printf( "something wrong!" );<br /><br />原因：<br />error只有在调用失败的时候才会被修改，调用成功的时候不会被修改；<br />当errno = 0 时，不能确定调用成功，也有可能是前面的代码把errno设置成了0；<br />当errno &lt; 0 时，不能确定调用失败，也有可能是上次调用失败遗留下来的结果。<br /><img src ="http://www.cppblog.com/continue123456/aggbug/201570.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/continue123456/" target="_blank">烽火依然</a> 2013-07-06 19:21 <a href="http://www.cppblog.com/continue123456/archive/2013/07/06/201570.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>