﻿<?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++博客-程序人生-文章分类-C++</title><link>http://www.cppblog.com/liu1061/category/5732.html</link><description>生活和工作都要充满激情,否则你无法体会到淋漓尽致的快乐与痛苦</description><language>zh-cn</language><lastBuildDate>Fri, 29 Aug 2008 03:22:32 GMT</lastBuildDate><pubDate>Fri, 29 Aug 2008 03:22:32 GMT</pubDate><ttl>60</ttl><item><title>总结自己写程序时的变量，函数命名风格！</title><link>http://www.cppblog.com/liu1061/articles/60311.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Thu, 28 Aug 2008 16:25:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/60311.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/60311.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/60311.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/60311.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/60311.html</trackback:ping><description><![CDATA[<p><br>总结自己写程序时的变量，函数命名风格！<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;example :<br><span style="FONT-FAMILY: courier new">pointer --&gt; p_+varname;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int *p_ivalue = NULL; <br>typedef --&gt; t_+typenam;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typedef struct name t_name<br>globle variable --&gt; g_+typevarname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int g_ivalue&nbsp; = 0;<br>member variable --&gt; m_+typevarname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int m_ivalue&nbsp; = 0;<br>static variable --&gt; s_+typevarname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static int s_ivalue = 0;<br>temp variable&nbsp;&nbsp; --&gt; typevarname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int ivalue = 0;<br>string varable&nbsp; --&gt; str_varname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string str_name;<br>const string varalbe --&gt; cstr_varname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const string cstr_name</span></p>
<p style="FONT-FAMILY: courier new"><br>function style :<br>extern int name_name_name(type var_name,...)</p>
<img src ="http://www.cppblog.com/liu1061/aggbug/60311.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2008-08-29 00:25 <a href="http://www.cppblog.com/liu1061/articles/60311.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C常用宏定义！！！</title><link>http://www.cppblog.com/liu1061/articles/54896.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Sun, 29 Jun 2008 02:06:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/54896.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/54896.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/54896.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/54896.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/54896.html</trackback:ping><description><![CDATA[<div id=art style="FONT-SIZE: 10pt; MARGIN: 15px; FONT-FAMILY: courier new" width="100%"><span style="FONT-WEIGHT: bold">01:</span> 防止一个头文件被重复包含<br>#ifndef COMDEF_H<br>#define COMDEF_H<br>//头文件内容<br>#endif<br><span style="FONT-WEIGHT: bold">02: </span>重新定义一些类型,防止由于各种平台和编译器的不同,而产生的类型字节数差异,方便移植。<br>typedef&nbsp; unsigned char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean;&nbsp;&nbsp;&nbsp;&nbsp; /* Boolean value type. */<br>typedef&nbsp; unsigned long int&nbsp; uint32;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 32 bit value */<br>typedef&nbsp; unsigned short&nbsp;&nbsp;&nbsp;&nbsp; uint16;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 16 bit value */<br>typedef&nbsp; unsigned char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint8;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 8&nbsp; bit value */<br>typedef&nbsp; signed long int&nbsp;&nbsp;&nbsp; int32;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 32 bit value */<br>typedef&nbsp; signed short&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int16;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 16 bit value */<br>typedef&nbsp; signed char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int8;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 8&nbsp; bit value */<br><br>//下面的不建议使用<br>typedef&nbsp; unsigned char&nbsp;&nbsp;&nbsp;&nbsp; byte;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 8&nbsp; bit value type. */<br>typedef&nbsp; unsigned short&nbsp;&nbsp;&nbsp; word;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsinged 16 bit value type. */<br>typedef&nbsp; unsigned long&nbsp;&nbsp;&nbsp;&nbsp; dword;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 32 bit value type. */<br>typedef&nbsp; unsigned char&nbsp;&nbsp;&nbsp;&nbsp; uint1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 8&nbsp; bit value type. */<br>typedef&nbsp; unsigned short&nbsp;&nbsp;&nbsp; uint2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 16 bit value type. */<br>typedef&nbsp; unsigned long&nbsp;&nbsp;&nbsp;&nbsp; uint4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Unsigned 32 bit value type. */<br>typedef&nbsp; signed char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 8&nbsp; bit value type. */<br>typedef&nbsp; signed short&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 16 bit value type. */<br>typedef&nbsp; long int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int4;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 32 bit value type. */<br>typedef&nbsp; signed long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sint31;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 32 bit value */<br>typedef&nbsp; signed short&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sint15;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 16 bit value */<br>typedef&nbsp; signed char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sint7;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* Signed 8&nbsp; bit value */<br><br><span style="FONT-WEIGHT: bold">03: </span>得到指定地址上的一个字节或字<br>#define&nbsp; MEM_B(x) (*((byte *)(x)))<br>#define&nbsp; MEM_W(x) (*((word *)(x)))<br><br><span style="FONT-WEIGHT: bold">04: </span>求最大值和最小值<br>#define&nbsp; MAX(x,y) (((x)&gt;(y)) ? (x) : (y))<br>#define&nbsp; MIN(x,y) (((x) &lt; (y)) ? (x) : (y))<br><br><span style="FONT-WEIGHT: bold">05: </span>得到一个field在结构体(struct)中的偏移量<br>#define FPOS(type,field) ((dword)&amp;((type *)0)-&gt;field)<br><br><span style="FONT-WEIGHT: bold">06: </span>得到一个结构体中field所占用的字节数<br>#define FSIZ(type,field) sizeof(((type *)0)-&gt;field)<br><br><span style="FONT-WEIGHT: bold">07: </span>按照LSB格式把两个字节转化为一个Word<br>#define FLIPW(ray) ((((word)(ray)[0]) * 256) + (ray)[1])<br><br><span style="FONT-WEIGHT: bold">08: </span>按照LSB格式把一个Word转化为两个字节<br>#define FLOPW(ray,val) (ray)[0] = ((val)/256); (ray)[1] = ((val) &amp; 0xFF)<br><br><span style="FONT-WEIGHT: bold">09:</span> 得到一个变量的地址（word宽度）<br>#define B_PTR(var)&nbsp; ((byte *) (void *) &amp;(var))<br>#define W_PTR(var)&nbsp; ((word *) (void *) &amp;(var))<br><br><span style="FONT-WEIGHT: bold">10:</span> 得到一个字的高位和低位字节<br>#define WORD_LO(xxx)&nbsp; ((byte) ((word)(xxx) &amp; 255))<br>#define WORD_HI(xxx)&nbsp; ((byte) ((word)(xxx) &gt;&gt; 8))<br><br><span style="FONT-WEIGHT: bold">11: </span>返回一个比X大的最接近的8的倍数<br>#define RND8(x) ((((x) + 7)/8) * 8)<br><br><span style="FONT-WEIGHT: bold">12:</span> 将一个字母转换为大写<br>#define UPCASE(c) (((c)&gt;='a' &amp;&amp; (c) &lt;= 'z') ? ((c) - 0x20) : (c))<br><br style="FONT-WEIGHT: bold"><span style="FONT-WEIGHT: bold">13:</span> 判断字符是不是10进值的数字<br>#define&nbsp; DECCHK(c) ((c)&gt;='0' &amp;&amp; (c)&lt;='9')<br><br style="FONT-WEIGHT: bold"><span style="FONT-WEIGHT: bold">14:</span> 判断字符是不是16进值的数字<br>#define HEXCHK(c) (((c) &gt;= '0' &amp;&amp; (c)&lt;='9') ((c)&gt;='A' &amp;&amp; (c)&lt;= 'F') \<br>((c)&gt;='a' &amp;&amp; (c)&lt;='f'))<br><br style="FONT-WEIGHT: bold"><span style="FONT-WEIGHT: bold">15:</span> 防止溢出的一个方法<br>#define INC_SAT(val) (val=((val)+1&gt;(val)) ? (val)+1 : (val))<br><br style="FONT-WEIGHT: bold"><span style="FONT-WEIGHT: bold">16:</span> 返回数组元素的个数<br>#define ARR_SIZE(a)&nbsp; (sizeof((a))/sizeof((a[0])))<br><br><span style="FONT-WEIGHT: bold">17:</span> 返回一个无符号数n尾的值MOD_BY_POWER_OF_TWO(X,n)=X%(2^n)<br>#define MOD_BY_POWER_OF_TWO( val, mod_by ) ((dword)(val) &amp; (dword)((mod_by)-1))<br><br><span style="FONT-WEIGHT: bold">18:</span> 对于IO空间映射在存储空间的结构,输入输出处理<br>#define inp(port) (*((volatile byte *)(port)))<br>#define inpw(port) (*((volatile word *)(port)))<br>#define inpdw(port) (*((volatile dword *)(port)))<br>#define outp(port,val) (*((volatile byte *)(port))=((byte)(val)))<br>#define outpw(port, val) (*((volatile word *)(port))=((word)(val)))<br>#define outpdw(port, val) (*((volatile dword *)(port))=((dword)(val)))<br><br><span style="FONT-WEIGHT: bold">19:</span> 使用一些宏跟踪调试<br>ANSI标准说明了五个预定义的宏名。它们是：<br>__LINE__<br>__FILE__<br>__DATE__<br>__TIME__<br>__STDC__<br>C++中还定义了 __cplusplus<br><br>如果编译器不是标准的,则可能仅支持以上宏名中的几个,或根本不支持。记住编译程序也许还提供其它预定义的宏名。<br><br>__LINE__ 及 __FILE__ 宏指示，#line指令可以改变它的值，简单的讲，编译时，它们包含程序的当前行数和文件名。<br><br>__DATE__ 宏指令含有形式为月/日/年的串,表示源文件被翻译到代码时的日期。<br>__TIME__ 宏指令包含程序编译的时间。时间用字符串表示，其形式为： 分：秒<br>__STDC__ 宏指令的意义是编译时定义的。一般来讲，如果__STDC__已经定义，编译器将仅接受不包含任何非标准扩展的标准C/C++代码。如果实现是标准的,则宏__STDC__含有十进制常量1。如果它含有任何其它数,则实现是非标准的。<br>__cplusplus 与标准c++一致的编译器把它定义为一个包含至少6为的数值。与标准c++不一致的编译器将使用具有5位或更少的数值。<br><br><br>可以定义宏,例如:<br>当定义了_DEBUG,输出数据信息和所在文件所在行<br>#ifdef _DEBUG<br>#define DEBUGMSG(msg,date) printf(msg);printf(&#8220;%d%d%d&#8221;,date,_LINE_,_FILE_)<br>#else<br>#define DEBUGMSG(msg,date) <br>#endif<br>&nbsp;<br><br><span style="FONT-WEIGHT: bold">20：</span> 宏定义防止错误使用小括号包含。<br>例如：<br>有问题的定义：#define DUMP_WRITE(addr,nr) {memcpy(bufp,addr,nr); bufp += nr;}<br>应该使用的定义： #difne DO(a,b) do{a+b;a++;}while(0)<br>例如：<br>if(addr)<br>&nbsp;&nbsp;&nbsp; DUMP_WRITE(addr,nr);<br>else <br>&nbsp;&nbsp;&nbsp; do_somethong_else();<br>宏展开以后变成这样:<br>if(addr)<br>&nbsp;&nbsp;&nbsp; {memcpy(bufp,addr,nr); bufp += nr;};<br>else<br>&nbsp;&nbsp;&nbsp; do_something_else();<br><br>gcc在碰到else前面的&#8220;；&#8221;时就认为if语句已经结束，因而后面的else不在if语句中。而采用do{} while(0)的定义，在任何情况下都没有问题。而改为 #difne DO(a,b) do{a+b;a++;}while(0) 的定义则在任何情况下都不会出错。<br></div>
<img src ="http://www.cppblog.com/liu1061/aggbug/54896.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2008-06-29 10:06 <a href="http://www.cppblog.com/liu1061/articles/54896.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>#define、const、typedef的差别(转)</title><link>http://www.cppblog.com/liu1061/articles/38613.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Sun, 16 Dec 2007 06:10:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38613.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38613.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38613.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38613.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38613.html</trackback:ping><description><![CDATA[<p>#define 并不是定义变量啊</p>
<p>#define 只是用来做文本替换的</p>
<p>例如：</p>
<p>#define Pi 3.1415926</p>
<p>float angel;</p>
<p>angel=30*Pi/180;</p>
<p>那么，当程序进行编译的时候,编译器会首先将 &#8220;#define Pi 3.1415926&#8221;以后的，所有代码中的&#8220;Pi&#8221;全部换成 &#8220;3.1415926&#8221;</p>
<p>然后再进行编译。</p>
<p>&nbsp;</p>
<p><font color=#800000>我查到一个讲const与#define的差别的帖子，里面谈到const与#define最大的差别在于：前者在堆栈分配了空间，而后者只是把具体数值直接传递到目标变量罢了。或者说，const的常量是一个Run-Time的概念，他在程序中确确实实的存在并可以被调用、传递。而#define常量则是一个Compile-Time概念，它的生命周期止于编译期：在实际程序中他只是一个常数、一个命令中的参数，没有实际的存在。 <br></font></p>
<p><font color=#800000>const常量存在于程序的数据段，#define常量存在于程序的代码段。</font></p>
<p><font color=#800000>至于两者的优缺点，要看具体的情况了。一般的常数应用，笔者个人认为#define是一个更好的选择：</font></p>
<p><font color=#800000>i.从run-time的角度来看，他在空间上和时间上都有很好优势。</font></p>
<p><font color=#800000>ii.从compile-time的角度来看，类似m=t*10的代码不会被编译器优化，t*10的操作需要在run-time执行。而#define的常量会被合并（在上例中T*10将被0x82取代）。</font></p>
<p><font color=#800000>但是：如果你需要粗鲁的修改常数的值，那就得使用const了，因为后者在程序中没有实际的存在。（其实应该说修改数据段比代码段要简单^_^）。</font></p>
<p><strong><font style="BACKGROUND-COLOR: #ffff99" color=#000000 size=5>&nbsp;有关#define的用法</font></strong></p>
<p><font color=#0000ff><span style="FONT-SIZE: 10pt">1.</span><span style="FONT-SIZE: 10pt">简单的</span><span style="FONT-SIZE: 10pt">define</span><span style="FONT-SIZE: 10pt">定义</span></font><span style="FONT-SIZE: 10pt"><br>#define MAXTIME 1000 </span></p>
<p><span style="FONT-SIZE: 10pt"></span><font color=#ff0000><span style="FONT-SIZE: 10pt">程序中遇到</span><span style="FONT-SIZE: 10pt">MAXTIME</span><span style="FONT-SIZE: 10pt">就会当作</span><span style="FONT-SIZE: 10pt">1000</span><span style="FONT-SIZE: 10pt">来处理</span></font><span style="FONT-SIZE: 10pt"><font color=#ff0000>.<br></font></span><span style="FONT-SIZE: 10pt">一个简单的</span><span style="FONT-SIZE: 10pt">MAXTIME</span><span style="FONT-SIZE: 10pt">就定义好了，它代表</span><span style="FONT-SIZE: 10pt">1000</span><span style="FONT-SIZE: 10pt">，如果在程序里面写</span><span style="FONT-SIZE: 10pt"><br>if(i&lt;MAXTIME){.........}<br></span><span style="FONT-SIZE: 10pt">编译器在处理这个代码之前会对</span><span style="FONT-SIZE: 10pt">MAXTIME</span><span style="FONT-SIZE: 10pt">进行处理替换为</span><span style="FONT-SIZE: 10pt">1000</span><span style="FONT-SIZE: 10pt">。</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt; COLOR: brown">这样的定义看起来类似于普通的常量定义</span><span style="FONT-SIZE: 10pt; COLOR: brown">CONST</span><span style="FONT-SIZE: 10pt; COLOR: brown">，但也有着不同，因为</span><span style="FONT-SIZE: 10pt; COLOR: brown">define</span><span style="FONT-SIZE: 10pt; COLOR: brown">的定义更像是简单的文本替换，而不是作为一个量来使用，这个问题在下面反映的尤为突出。</span></p>
<div><font color=#0000ff><span style="FONT-SIZE: 10pt">2.define</span><span style="FONT-SIZE: 10pt">的</span><span style="FONT-SIZE: 10pt">&#8220;</span><span style="FONT-SIZE: 10pt">函数定义</span></font><span style="FONT-SIZE: 10pt"><font color=#0000ff>&#8221;<br></font>define</span><span style="FONT-SIZE: 10pt">可以像函数那样接受一些参数，如下</span><span style="FONT-SIZE: 10pt"><br>#define max(x,y) (x)&gt;(y)?(x):(y);<br></span><span style="FONT-SIZE: 10pt">这个定义就将返回两个数中较大的那个，看到了吗？因为这个</span><span style="FONT-SIZE: 10pt">&#8220;</span><span style="FONT-SIZE: 10pt">函数</span><span style="FONT-SIZE: 10pt">&#8221;</span><span style="FONT-SIZE: 10pt">没有类型检查，就好像一个函数模板似的，当然，它绝对没有模板那么安全就是了。可以作为一个简单的模板来使用而已。</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt; COLOR: purple">但是这样做的话存在隐患</span><span style="FONT-SIZE: 10pt">，例子如下：</span><span style="FONT-SIZE: 10pt"><br>#define Add(a,b) a+b;<br></span><span style="FONT-SIZE: 10pt">在一般使用的时候是没有问题的，但是如果遇到如：</span><span style="FONT-SIZE: 10pt">c * Add(a,b) * d</span><span style="FONT-SIZE: 10pt">的时候就会出现问题，代数式的本意是</span><span style="FONT-SIZE: 10pt">a+b</span><span style="FONT-SIZE: 10pt">然后去和</span><span style="FONT-SIZE: 10pt">c</span><span style="FONT-SIZE: 10pt">，</span><span style="FONT-SIZE: 10pt">d</span><span style="FONT-SIZE: 10pt">相乘，但是因为使用了</span><span style="FONT-SIZE: 10pt">define</span><span style="FONT-SIZE: 10pt">（它只是一个简单的替换），所以式子实际上变成了</span><span style="FONT-SIZE: 10pt"><br>c*a + b*d<br></span><span style="FONT-SIZE: 10pt">另外举一个例子：</span><span style="FONT-SIZE: 10pt"><br>#define pin (int*);<br>pin a,b;<br></span><span style="FONT-SIZE: 10pt">本意是</span><span style="FONT-SIZE: 10pt">a</span><span style="FONT-SIZE: 10pt">和</span><span style="FONT-SIZE: 10pt">b</span><span style="FONT-SIZE: 10pt">都是</span><span style="FONT-SIZE: 10pt">int</span><span style="FONT-SIZE: 10pt">型指针，但是实际上变成</span><span style="FONT-SIZE: 10pt">int* a,b;<br>a</span><span style="FONT-SIZE: 10pt">是</span><span style="FONT-SIZE: 10pt">int</span><span style="FONT-SIZE: 10pt">型指针，而</span><span style="FONT-SIZE: 10pt">b</span><span style="FONT-SIZE: 10pt">是</span><span style="FONT-SIZE: 10pt">int</span><span style="FONT-SIZE: 10pt">型变量。</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">这时应该使用</span><span style="FONT-SIZE: 10pt">typedef</span><span style="FONT-SIZE: 10pt">来代替</span><span style="FONT-SIZE: 10pt">define</span><span style="FONT-SIZE: 10pt">，这样</span><span style="FONT-SIZE: 10pt">a</span><span style="FONT-SIZE: 10pt">和</span><span style="FONT-SIZE: 10pt">b</span><span style="FONT-SIZE: 10pt">就都是</span><span style="FONT-SIZE: 10pt">int</span><span style="FONT-SIZE: 10pt">型指针了。</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">所以我们在定义的时候，养成一个良好的习惯，建议所有的层次都要加括号。</span><span style="FONT-SIZE: 10pt"><br><font color=#0000ff>3.</font></span><span style="FONT-SIZE: 10pt"><font color=#0000ff>宏的单行定义</font></span><span style="FONT-SIZE: 10pt"><br>#define A(x) T_##x<br>#define B</span><span style="FONT-SIZE: 10pt">（</span><span style="FONT-SIZE: 10pt">x</span><span style="FONT-SIZE: 10pt">）</span><span style="FONT-SIZE: 10pt"> #@x<br>#define C</span><span style="FONT-SIZE: 10pt">（</span><span style="FONT-SIZE: 10pt">x</span><span style="FONT-SIZE: 10pt">）</span><span style="FONT-SIZE: 10pt"> #x<br></span><span style="FONT-SIZE: 10pt">我们假设：</span><span style="FONT-SIZE: 10pt">x=1</span><span style="FONT-SIZE: 10pt">，则有：</span><span style="FONT-SIZE: 10pt"><br>A(1)------</span><span style="FONT-SIZE: 10pt">）</span><span style="FONT-SIZE: 10pt">T_1<br>B(1)------</span><span style="FONT-SIZE: 10pt">）</span><span style="FONT-SIZE: 10pt">'1'<br>C(1)------</span><span style="FONT-SIZE: 10pt">）</span><span style="FONT-SIZE: 10pt">"1"</span><span style="FONT-SIZE: 10pt"><br><font color=#0000ff>4.define</font></span><span style="FONT-SIZE: 10pt"><font color=#0000ff>的多行定义</font></span><span style="FONT-SIZE: 10pt"><br>define</span><span style="FONT-SIZE: 10pt">可以替代多行的代码，例如</span><span style="FONT-SIZE: 10pt">MFC</span><span style="FONT-SIZE: 10pt">中的宏定义（非常的经典，虽然让人看了恶心）</span><span style="FONT-SIZE: 10pt"><br>#define MACRO(arg1, arg2)&nbsp;do { \<br>/* declarations */ \<br>stmt1; \<br>stmt2; \<br>/* ... */ \<br>} while(0) /* (no trailing ; ) */<br></span><span style="FONT-SIZE: 10pt">关键是要在每一个换行的时候加上一个</span><span style="FONT-SIZE: 10pt">"\" <br></span><span style="FONT-SIZE: 10pt">摘抄自</span><span style="FONT-SIZE: 10pt"><a href="http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml" target=_blank><span style="COLOR: purple">http://www.blog.edu.cn/user1/16293/archives/2005/115370.shtml</span></a> </span><span style="FONT-SIZE: 10pt">修补了几个</span><span style="FONT-SIZE: 10pt">bug<br><font color=#0000ff>5.</font></span><font color=#0000ff><span style="FONT-SIZE: 10pt">在大规模的开发过程中，特别是跨平台和系统的软件里，</span><span style="FONT-SIZE: 10pt">define</span></font><span style="FONT-SIZE: 10pt"><font color=#0000ff>最重要的功能是条件编译</font></span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">就是：</span><span style="FONT-SIZE: 10pt"><br>#ifdef WINDOWS<br>......<br>......<br>#endif<br>#ifdef LINUX<br>......<br>......<br>#endif<br><br></span><span style="FONT-SIZE: 10pt">可以在编译的时候通过</span><span style="FONT-SIZE: 10pt">#define</span><span style="FONT-SIZE: 10pt">设置编译环境</span><span style="FONT-SIZE: 10pt"><br><br><font color=#0000ff>6.</font></span><span style="FONT-SIZE: 10pt"><font color=#0000ff>如何定义宏、取消宏</font></span><span style="FONT-SIZE: 10pt"><br>//</span><span style="FONT-SIZE: 10pt">定义宏</span><span style="FONT-SIZE: 10pt"><br>#define [MacroName] [MacroValue]<br>//</span><span style="FONT-SIZE: 10pt">取消宏</span><span style="FONT-SIZE: 10pt"><br>#undef [MacroName]<br></span><span style="FONT-SIZE: 10pt">普通宏</span><span style="FONT-SIZE: 10pt"><br>#define PI (3.1415926)<br></span><span style="FONT-SIZE: 10pt">带参数的宏</span><span style="FONT-SIZE: 10pt"><br>#define max(a,b) ((a)&gt;(b)? (a),(b))<br></span><span style="FONT-SIZE: 10pt">关键是十分容易产生错误，包括机器和人理解上的差异等等。</span><span style="FONT-SIZE: 10pt"><br><font color=#0000ff>7.</font></span><span style="FONT-SIZE: 10pt"><font color=#0000ff>条件编译</font></span><span style="FONT-SIZE: 10pt"><br>#ifdef XXX&#8230;(#else) &#8230;#endif<br></span><span style="FONT-SIZE: 10pt">例如</span><span style="FONT-SIZE: 10pt"> #ifdef DV22_AUX_INPUT<br>#define AUX_MODE 3 <br>#else<br>#define AUY_MODE 3<br></span><span style="FONT-SIZE: 10pt">＃</span><span style="FONT-SIZE: 10pt">endif<br>#ifndef XXX &#8230; (#else) &#8230; #endif<br><br><font color=#0000ff>8.</font></span><font color=#0000ff><span style="FONT-SIZE: 10pt">头文件</span><span style="FONT-SIZE: 10pt">(.h)</span><span style="FONT-SIZE: 10pt">可以被头文件或</span><span style="FONT-SIZE: 10pt">C</span><span style="FONT-SIZE: 10pt">文件包含</span></font><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">重复包含（重复定义）</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">由于头文件包含可以嵌套，那么</span><span style="FONT-SIZE: 10pt">C</span><span style="FONT-SIZE: 10pt">文件就有可能包含多次同一个头文件，就可能出现重复定义的问题的。</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">通过条件编译开关来避免重复包含（重复定义）</span><span style="FONT-SIZE: 10pt"><br></span><span style="FONT-SIZE: 10pt">例如</span><span style="FONT-SIZE: 10pt"><br>#ifndef __headerfileXXX__<br></span><span style="FONT-SIZE: 10pt">＃</span><span style="FONT-SIZE: 10pt">define __headerfileXXX__<br>&#8230;<br></span><span style="FONT-SIZE: 10pt">文件内容</span><span style="FONT-SIZE: 10pt"><br>&#8230;<br>#endif</span></div>
<div><span style="FONT-SIZE: 10pt"></span>&nbsp;</div>
<div><span style="FONT-SIZE: 10pt"><font style="BACKGROUND-COLOR: #ffff99" color=#0000ff size=5><strong>typedef和#define的用法与区别</strong></font></span></div>
<p>&nbsp;</p>
<p><strong>一、typedef的用法</strong></p>
<p>在C/C++语言中，typedef常用来定义一个标识符及关键字的别名，它是语言编译过程的一部分，但它并不实际分配内存空间，实例像：</p>
<p>typedef&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INT;<br>typedef&nbsp;&nbsp;&nbsp; int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ARRAY[10];<br>typedef&nbsp;&nbsp; (int*)&nbsp;&nbsp;&nbsp; pINT;</p>
<p>typedef可以增强程序的可读性，以及标识符的灵活性，但它也有&#8220;非直观性&#8221;等缺点。</p>
<p><strong>二、#define的用法</strong></p>
<p>#define为一宏定义语句，通常用它来定义常量(包括无参量与带参量)，以及用来实现那些&#8220;表面似和善、背后一长串&#8221;的宏，它本身并不在编译过程中进行，而是在这之前(预处理过程)就已经完成了，但也因此难以发现潜在的错误及其它代码维护问题，它的实例像：</p>
<p>#define&nbsp;&nbsp; INT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int<br>#define&nbsp;&nbsp; TRUE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1<br>#define&nbsp;&nbsp; Add(a,b)&nbsp;&nbsp;&nbsp;&nbsp; ((a)+(b));<br>#define&nbsp;&nbsp; Loop_10&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;10; i++)</p>
<p>在Scott Meyer的Effective C++一书的条款1中有关于#define语句弊端的分析，以及好的替代方法，大家可参看。</p>
<p><strong>三、typedef与#define的区别</strong></p>
<p>从以上的概念便也能基本清楚，typedef只是为了增加可读性而为标识符另起的新名称(仅仅只是个别名)，而#define原本在C中是为了定义常量，到了C++，const、enum、inline的出现使它也渐渐成为了起别名的工具。有时很容易搞不清楚与typedef两者到底该用哪个好，如#define INT int这样的语句，用typedef一样可以完成，用哪个好呢？我主张用typedef，因为在早期的许多C编译器中这条语句是非法的，只是现今的编译器又做了扩充。为了尽可能地兼容，一般都遵循#define定义&#8220;可读&#8221;的常量以及一些宏语句的任务，而typedef则常用来定义关键字、冗长的类型的别名。</p>
<p>宏定义只是简单的字符串代换(原地扩展)，而typedef则不是原地扩展，它的新名字具有一定的封装性，以致于新命名的标识符具有更易定义变量的功能。请看上面第一大点代码的第三行：</p>
<p>typedef&nbsp;&nbsp;&nbsp; (int*)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pINT;<br>以及下面这行:<br>#define&nbsp;&nbsp;&nbsp; pINT2&nbsp;&nbsp;&nbsp; int*</p>
<p>效果相同？实则不同！实践中见差别：pINT a,b;的效果同int *a; int *b;表示定义了两个整型指针变量。而pINT2 a,b;的效果同int *a, b;</p>
<p>表示定义了一个整型指针变量a和整型变量b。</p>
<p><font color=#ff0000>注意：两者还有一个行尾;号的区别哦！</font></p>
<img src ="http://www.cppblog.com/liu1061/aggbug/38613.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-16 14:10 <a href="http://www.cppblog.com/liu1061/articles/38613.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>明晰C++内存分配的五种方法的区别(转）</title><link>http://www.cppblog.com/liu1061/articles/38610.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Sun, 16 Dec 2007 05:44:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38610.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38610.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38610.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38610.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38610.html</trackback:ping><description><![CDATA[<p><span class=oblog_text><strong><font size=2>在C++中，内存分成5个区，他们分别是堆、栈、自由存储区、全局/静态存储区和常量存储区。</font></strong>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Origin:http://www.zhmy.com/blog2006004/goto723/archives/2006/29705.shtml</p>
<p><br>　　<strong><font color=#ff0000 size=3>栈</font></strong>，就是那些由编译器在需要的时候分配，在不需要的时候自动清除的变量的存储区。里面的变量通常是局部变量、函数参数等。 <br><br>　　<strong><font color=#ff0000 size=3>堆</font></strong>，就是那些由new分配的内存块，它们的释放编译器不管，而是由程序员自己去控制，一般一个new就要对应一个delete。如果程序员没有释放掉，那么在程序结束后，操作系统会自动回收。 <br><br>　　<strong><font color=#ff0000 size=3>自由存储区</font></strong>，就是那些由malloc等分配的内存块，它和堆是十分相似的，不过它是用free来结束自己的生命的。 <br><br>　　<font color=#ff0000 size=3><strong>全局/静态存储区</strong></font>，全局变量和静态变量被分配到同一块内存中，在以前的C语言中，全局变量又分为初始化的和未初始化的，在C++里面没有这个区分了，他们共同占用同一块内存区。 <br><br>　　<font color=#ff0000 size=3><strong>常量存储区</strong></font>，这是一块比较特殊的存储区，它们里面存放的是常量，不允许修改（当然，你要通过非正当手段也可以修改，而且方法很多，在《const的思考》一文中，我给出了6种方法） <br><strong><font color=#0000ff size=3></font></strong></span></p>
<p><span class=oblog_text><strong><font color=#0000ff size=3>明确区分堆与栈</font></strong> <br><br>　　在bbs上，堆与栈的区分问题，似乎是一个永恒的话题，由此可见，初学者对此往往是混淆不清的，所以我决定拿它第一个开刀。 <br><br>　　首先，我们举一个例子： <br><br>void f() { </span></p>
<p><span class=oblog_text>int* p=new int[5]; </span></p>
<p><span class=oblog_text>} <br><br>　　这条短短的一句话就包含了堆与栈，看到new，我们首先就应该想到，我们分配了一块堆内存，那么指针p呢？它分配的是一块栈内存，所以这句话的意思就是：在栈内存中存放了一个指向一块堆内存的指针p。</span></p>
<p><span class=oblog_text>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在程序会先确定在堆中分配内存的大小，然后调用operator new分配内存，然后返回这块内存的首地址，放入栈中，他在VC6下的汇编代码如下： <br><br><strong><font color=#800000 size=2>00401028 push 14h <br>0040102A call operator new (00401060) <br>0040102F add esp,4 <br>00401032 mov dword ptr [ebp-8],eax <br>00401035 mov eax,dword ptr [ebp-8] <br>00401038 mov dword ptr [ebp-4],eax</font></strong> <br><br>　　这里，我们为了简单并没有释放内存，那么该怎么去释放呢？是delete p么？错了，应该是delete []p，这是为了告诉编译器：我删除的是一个数组，VC6就会根据相应的Cookie信息去进行释放内存的工作。 <br><br>　　好了，我们回到我们的主题：堆和栈究竟有什么区别？ <br><br>　　主要的区别由以下几点： <br><br>　　1、管理方式不同； <br><br>　　2、空间大小不同； <br><br>　　3、能否产生碎片不同； <br><br>　　4、生长方向不同； <br><br>　　5、分配方式不同； <br><br>　　6、分配效率不同； <br><br>　　<strong><font color=#ff0000 size=3>管理方式</font></strong>：对于栈来讲，是由编译器自动管理，无需我们手工控制；对于堆来说，释放工作由程序员控制，容易产生memory leak。 <br><br>　　<strong><font color=#ff0000 size=3>空间大小</font></strong>：一般来讲在32位系统下，堆内存可以达到4G的空间，从这个角度来看堆内存几乎是没有什么限制的。但是对于栈来讲，一般都是有一定的空间大小的，例如，在VC6下面，默认的栈空间大小是1M（好像是，记不清楚了）。当然，我们可以修改： <br><br>　　打开工程，依次操作菜单如下：Project-&gt;Setting-&gt;Link，在Category 中选中Output，然后在Reserve中设定堆栈的最大值和commit。 <br><br>　　注意：Reserve最小值为4Byte；commit是保留在虚拟内存的页文件里面，它设置的较大会使栈开辟较大的值，可能增加内存的开销和启动时间。 <br><br>　　<font color=#ff0000 size=3><strong>碎片问题</strong></font>：对于堆来讲，频繁的new/delete势必会造成内存空间的不连续，从而造成大量的碎片，使程序效率降低。对于栈来讲，则不会存在这个问题，因为栈是先进后出的队列，他们是如此的一一对应，以至于永远都不可能有一个内存块从栈中间弹出，在它弹出之前，在它上面的后进的栈内容已经被弹出，详细的可以参考数据结构，这里我们就不再一一讨论了。 <br><br>　　<strong><font color=#ff0000 size=3>生长方向</font></strong>：<font style="BACKGROUND-COLOR: #33cccc">对于堆来讲，生长方向是向上的，也就是向着内存地址增加的方向；对于栈来讲，它的生长方向是向下的，是向着内存地址减小的方向增长。</font> <br><br>　　<strong><font color=#ff0000 size=3>分配方式</font></strong>：堆都是动态分配的，没有静态分配的堆。栈有2种分配方式：静态分配和动态分配。静态分配是编译器完成的，比如局部变量的分配。动态分配由alloca函数进行分配，但是栈的动态分配和堆是不同的，它的动态分配是由编译器进行释放，不需要我们手工实现。 <br><br>　　<font color=#ff0000 size=3><strong>分配效率</strong></font>：栈是机器系统提供的数据结构，计算机会在底层对栈提供支持：分配专门的寄存器存放栈的地址，压栈出栈都有专门的指令执行，这就决定了栈的效率比较高(<strong>我的注释:</strong><font style="BACKGROUND-COLOR: #00ccff">关于EBP寄存器请参考另一篇文章</font>)。</span></p>
<p><span class=oblog_text>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 堆则是C/C++函数库提供的，它的机制是很复杂的，例如为了分配一块内存，库函数会按照一定的算法（具体的算法可以参考数据结构/操作系统）在堆内存中搜索可用的足够大小的空间，如果没有足够大小的空间（可能是由于内存碎片太多），就有可能调用系统功能去增加程序数据段的内存空间，这样就有机会分到足够大小的内存，然后进行返回。显然，堆的效率比栈要低得多。<br><br>　　从这里我们可以看到，堆和栈相比，由于大量new/delete的使用，容易造成大量的内存碎片；由于没有专门的系统支持，效率很低；由于可能引发用户态和核心态的切换，内存的申请，代价变得更加昂贵。所以栈在程序中是应用最广泛的，就算是函数的调用也利用栈去完成，函数调用过程中的参数，返回地址，EBP和局部变量都采用栈的方式存放。所以，我们推荐大家尽量用栈，而不是用堆。 <br><br>　　虽然栈有如此众多的好处，但是由于和堆相比不是那么灵活，有时候分配大量的内存空间，还是用堆好一些。 <br><br>　　无论是堆还是栈，都要防止越界现象的发生（除非你是故意使其越界），因为越界的结果要么是程序崩溃，要么是摧毁程序的堆、栈结构，产生意想不到的结果,就算是在你的程序运行过程中，没有发生上面的问题，你还是要小心，说不定什么时候就崩掉，那时候debug可是相当困难的：）&nbsp;</span></p>
<img src ="http://www.cppblog.com/liu1061/aggbug/38610.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-16 13:44 <a href="http://www.cppblog.com/liu1061/articles/38610.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符数组VS字符指针</title><link>http://www.cppblog.com/liu1061/articles/38608.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Sun, 16 Dec 2007 05:29:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38608.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38608.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38608.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38608.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38608.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 9.5pt">1.</span><span style="FONT-SIZE: 9.5pt">以字符串形式出现的，编译器都会为该字符串自动添加一个</span><span style="FONT-SIZE: 9.5pt">0</span><span style="FONT-SIZE: 9.5pt">作为结束符，如在代码中写</span><span style="FONT-SIZE: 9.5pt">"abc",</span><span style="FONT-SIZE: 9.5pt">那么编译器帮你存储的是</span><span style="FONT-SIZE: 9.5pt">"abc\0"</span> <br><span style="FONT-SIZE: 9.5pt"><br>2."abc"</span><span style="FONT-SIZE: 9.5pt">是常量吗？答案是有时是，有时不是。<br></span><strong><span style="FONT-SIZE: 9.5pt; COLOR: blue">不是常量的情况</span></strong><span style="FONT-SIZE: 9.5pt">：<br></span><span style="FONT-SIZE: 9.5pt">&nbsp; "abc"</span><span style="FONT-SIZE: 9.5pt">作为字符数组初始值的时候就不是，如</span><span style="FONT-SIZE: 9.5pt">char str[] = "abc";</span><span style="FONT-SIZE: 9.5pt">因为定义的是一个字符数组，所以就相当于定义了一些空间来存放</span><span style="FONT-SIZE: 9.5pt">"abc"</span><span style="FONT-SIZE: 9.5pt">，而又因为字符数组就是把字符一个一个地存放的，所以编译器把这个语句解析为</span><span style="FONT-SIZE: 9.5pt">char str[3] = {'a','b','c'};</span><span style="FONT-SIZE: 9.5pt">又根据上面的总结</span><span style="FONT-SIZE: 9.5pt">1</span><span style="FONT-SIZE: 9.5pt">，所以</span><span style="FONT-SIZE: 9.5pt">char str[] = "abc";</span><span style="FONT-SIZE: 9.5pt">的最终结果是</span><span style="FONT-SIZE: 9.5pt">char str[4] = {'a','b','c','\0'};</span><span style="FONT-SIZE: 9.5pt">做一下扩展，如果</span><span style="FONT-SIZE: 9.5pt">char str[] = "abc";</span><span style="FONT-SIZE: 9.5pt">是在函数内部写的话，那么这里的</span><span style="FONT-SIZE: 9.5pt">"abc\0"</span><span style="FONT-SIZE: 9.5pt">因为不是常量，所以应该被放在栈上。<br></span><strong><span style="FONT-SIZE: 9.5pt; COLOR: blue">是常量的情况<br></span></strong><span style="FONT-SIZE: 9.5pt">&nbsp; 把</span><span style="FONT-SIZE: 9.5pt">"abc"</span><span style="FONT-SIZE: 9.5pt">赋给一个字符指针变量时，如</span><span style="FONT-SIZE: 9.5pt">char* ptr = "abc";</span><span style="FONT-SIZE: 9.5pt">因为定义的是一个普通指针，并没有定义空间来存放</span><span style="FONT-SIZE: 9.5pt">"abc"</span><span style="FONT-SIZE: 9.5pt">，所以编译器得帮我们找地方来放</span><span style="FONT-SIZE: 9.5pt">"abc"</span><span style="FONT-SIZE: 9.5pt">，显然，把这里的</span><span style="FONT-SIZE: 9.5pt">"abc"</span><span style="FONT-SIZE: 9.5pt">当成常量并把它放到程序的常量区是编译器最合适的选择。所以尽管</span><span style="FONT-SIZE: 9.5pt">ptr</span><span style="FONT-SIZE: 9.5pt">的类型不是</span><span style="FONT-SIZE: 9.5pt">const char*</span><span style="FONT-SIZE: 9.5pt">，并且</span><span style="FONT-SIZE: 9.5pt">ptr[0] = 'x';</span><span style="FONT-SIZE: 9.5pt">也能编译通过，但是执行</span><span style="FONT-SIZE: 9.5pt">ptr[0] = 'x';</span><span style="FONT-SIZE: 9.5pt">就会发生运行时异常，因为这个语句试图去修改程序常量区中的东西。记得哪本书中曾经说过</span><span style="FONT-SIZE: 9.5pt">char* ptr = "abc";</span><span style="FONT-SIZE: 9.5pt">这种写法原来在</span><span style="FONT-SIZE: 9.5pt">c++</span><span style="FONT-SIZE: 9.5pt">标准中是不允许的，但是因为这种写法在</span><span style="FONT-SIZE: 9.5pt">c</span><span style="FONT-SIZE: 9.5pt">中实在是太多了，为了兼容</span><span style="FONT-SIZE: 9.5pt">c</span><span style="FONT-SIZE: 9.5pt">，不允许也得允许。虽然允许，但是建议的写法应该是</span><span style="FONT-SIZE: 9.5pt">const char* ptr = "abc";</span><span style="FONT-SIZE: 9.5pt">这样如果后面写</span><span style="FONT-SIZE: 9.5pt">ptr[0] = 'x'</span><span style="FONT-SIZE: 9.5pt">的话编译器就不会让它编译通过，也就避免了上面说的运行时异常。又扩展一下，如果</span><span style="FONT-SIZE: 9.5pt">char* ptr = "abc";</span><span style="FONT-SIZE: 9.5pt">写在函数体内，那么虽然这里的</span><span style="FONT-SIZE: 9.5pt">"abc\0"</span><span style="FONT-SIZE: 9.5pt">被放在常量区中，但是</span><span style="FONT-SIZE: 9.5pt">ptr</span><span style="FONT-SIZE: 9.5pt">本身只是一个普通的指针变量，所以</span><span style="FONT-SIZE: 9.5pt">ptr</span><span style="FONT-SIZE: 9.5pt">是被放在栈上的</span><span style="FONT-SIZE: 9.5pt">,</span><span style="FONT-SIZE: 9.5pt">只不过是它所指向的东西被放在常量区罢了。<br><br>3.</span><span style="FONT-SIZE: 9.5pt">数组的类型是由该数组所存放的东西的类型以及数组本身的大小决定的。<br></span><span style="FONT-SIZE: 9.5pt">&nbsp; 如</span><span style="FONT-SIZE: 9.5pt">char s1[3]</span><span style="FONT-SIZE: 9.5pt">和</span><span style="FONT-SIZE: 9.5pt">char s2[4]</span><span style="FONT-SIZE: 9.5pt">，</span><span style="FONT-SIZE: 9.5pt">s1</span><span style="FONT-SIZE: 9.5pt">的类型就是</span><span style="FONT-SIZE: 9.5pt">char[3]</span><span style="FONT-SIZE: 9.5pt">，</span><span style="FONT-SIZE: 9.5pt">s2</span><span style="FONT-SIZE: 9.5pt">的类型就是</span><span style="FONT-SIZE: 9.5pt">char[4],</span><span style="FONT-SIZE: 9.5pt">也就是说尽管</span><span style="FONT-SIZE: 9.5pt">s1</span><span style="FONT-SIZE: 9.5pt">和</span><span style="FONT-SIZE: 9.5pt">s2</span><span style="FONT-SIZE: 9.5pt">都是字符数组，但两者的类型却是不同的。<br><br>4</span><span style="FONT-SIZE: 9.5pt">.</span><span style="FONT-SIZE: 9.5pt">字符串常量的类型可以理解为相应字符常量数组的类型，如</span><span style="FONT-SIZE: 9.5pt">"abcdef"</span><span style="FONT-SIZE: 9.5pt">的类型就可以看成是</span><span style="FONT-SIZE: 9.5pt">const char[7]<br><br>5</span><span style="FONT-SIZE: 9.5pt">.sizeof</span><span style="FONT-SIZE: 9.5pt">是用来求类型的字节数的。如</span><span style="FONT-SIZE: 9.5pt">int a;</span><span style="FONT-SIZE: 9.5pt">那么无论</span><span style="FONT-SIZE: 9.5pt">sizeof(int)</span><span style="FONT-SIZE: 9.5pt">或者是</span><span style="FONT-SIZE: 9.5pt">sizeof(a)</span><span style="FONT-SIZE: 9.5pt">都是等于</span><span style="FONT-SIZE: 9.5pt">4</span><span style="FONT-SIZE: 9.5pt">，因为</span><span style="FONT-SIZE: 9.5pt">sizeof(a)</span><span style="FONT-SIZE: 9.5pt">其实就是</span><span style="FONT-SIZE: 9.5pt">sizeof(type of a)<br><br>6</span><span style="FONT-SIZE: 9.5pt">.</span><span style="FONT-SIZE: 9.5pt">对于函数参数列表中的以数组类型书写的形式参数，编译器把其解释为普通的指针类型，如对于</span><span style="FONT-SIZE: 9.5pt">void func(char sa[100],int ia[20],char *p)</span><span style="FONT-SIZE: 9.5pt">则</span><span style="FONT-SIZE: 9.5pt">sa</span><span style="FONT-SIZE: 9.5pt">的类型为</span><span style="FONT-SIZE: 9.5pt">char*</span><span style="FONT-SIZE: 9.5pt">，</span><span style="FONT-SIZE: 9.5pt">ia</span><span style="FONT-SIZE: 9.5pt">的类型为</span><span style="FONT-SIZE: 9.5pt">int*</span><span style="FONT-SIZE: 9.5pt">，</span><span style="FONT-SIZE: 9.5pt">p</span><span style="FONT-SIZE: 9.5pt">的类型为</span><span style="FONT-SIZE: 9.5pt">char*</span><br>转自<a title="crazy wind`s  blogs" href="http://tb.blog.csdn.net/TrackBack.aspx?PostId=1548301">crazy wind`s blogs</a>
<img src ="http://www.cppblog.com/liu1061/aggbug/38608.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-16 13:29 <a href="http://www.cppblog.com/liu1061/articles/38608.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>文件操作小结</title><link>http://www.cppblog.com/liu1061/articles/38446.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Thu, 13 Dec 2007 09:37:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38446.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38446.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38446.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38446.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38446.html</trackback:ping><description><![CDATA[<p>文件操作总是用的不是很好,今天总结一下它可用的函数!<br><br>FILE *fp = NULL;</p>
<p>FILE * fopen(char *filename, char *openmode)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;打开文件</p>
<p>int fclose(FILE * fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关闭文件</p>
<p>int fseek(FILE *fp, long offset,&nbsp; int base)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;文件定位</p>
<p>void rewind(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;文件指针回到文件头</p>
<p>long ftell(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;返回fp所指文件读写位置</p>
<p>int fgetc(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从fp所指文件读1个char</p>
<p>char * fgets(char *buffer, int n, FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从fp所指文件读n个char</p>
<p>int fputc(char ch, FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;向fp所指文件写1个char</p>
<p>int fputs(char * str, FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;向fp所指文件写n个char</p>
<p>int fread(char *buffer, int size, int n, FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从fp所指文件读n个size到buffer</p>
<p>int fwrite(char *str,&nbsp; int&nbsp; size , int n, FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把str指向的n*size char写到文件</p>
<p>int fscanf(FILE *fp, char *format, args......)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从fp所指文件按format格式将数据读到args所指的内存单元内!</p>
<p>int fprintf(FILE *fp, char *format, args......)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;把args的值按format所指的格式写到fp所指的文件中</p>
<p>int feof(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;检查文件是否结束, 结束就反回非0值</p>
<p>int ferror(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;检查文件是否出错,没有出错时反回0</p>
<p>void clearerr(FILE *fp)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使fp所指的文件的错误,标志和文件结束等置<br></p>
<img src ="http://www.cppblog.com/liu1061/aggbug/38446.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-13 17:37 <a href="http://www.cppblog.com/liu1061/articles/38446.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C语言文件操作函数 转</title><link>http://www.cppblog.com/liu1061/articles/38441.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Thu, 13 Dec 2007 09:09:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38441.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38441.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38441.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38441.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38441.html</trackback:ping><description><![CDATA[<p><font color=#ff0000>文章分类"c/c++"里有，但只有基本说明，怕以后遗忘。看这篇相对详细点就留下做个备忘）<br></font></p>
<h2>C语言文件操作函数 <a href="http://www.cnblogs.com/nniixl/archive/2007/04/23/724235.aspx">http://www.cnblogs.com/nniixl/archive/2007/04/23/724235.aspx</a></h2>
<p><input id=search-key type=hidden value=C语言文件操作函数 name=search-key> </p>
<div class="rl entry-titlerl fenlei-Span">分类:<a href="http://yellowhong.bokee.com/cat.10508372.html"><font color=#000000>C/C </font></a></div>
<div class="entry-body nerr"><strong>13.1C语言文件<br></strong>1,两种文件存取方式(输入,输出方式)<br>顺序存取<br>直接存取<br>2,数据的两种存放形式<br>文本文件<br>二进制文件
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>13.2文件指针</strong><br>定义文件类型指针变量的一般形式:<br>FILE *指针变量名;<br>例如:<br>FILE *<a name=1></a><strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>1,*<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>2;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>13.3打开文件</strong><br>在使用文件之前,需打开文件.在C里使用fopen函数打开文件.格式为:<br>fopen(文件名,文件使用方式);<br>此函数返回一个指向FILE类型的指针.如:<br>FILE *<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>;<br><strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>=fopen("file_1","r");<br>如果调用成功,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>就指向file_1,否则返回为NULL,所以为了保证文件的正确使用,要进行测试.采用如下语句:<br>If((<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>=fopen("file_1","r"))==NULL)<br>{<br>printf("Cannot open this file\n");<br>exit(0);<br>}<br>最常用的文件使用方式及其含义如下:<br>1,"r".为读而打开文本文件.(不存在则出错)<br>2,"rb".为读而打开二进制文件.<br>3,"w".为写而打开文本文件.(若不存在则新建,反之,则从文件起始位置写,原内容将被覆盖)<br>4,"wb".为写而打开二进制文件.<br>5,"a".为在文件后面添加数据而打开文本文件.(若不存在,则新建;反之,在原文件后追加)<br>6,"ab".为在文件后面添加数据而打开一个二进制文件.<br>最常用的文件使用方式及其含义如下:<br>7,"r+".为读和写而打开文本文件.(读时,从头开始;在写数据时,新数据只覆盖所占的空间,其后不变)<br>8,"rb+".为读和写而打开二进制文件.只是在随后的读写时,可以由位置函数设置读和写的起始位置.<br>9,"w+".首先建立一个新文件,进行写操作,随后可以从头开始读.(若文件存在,原内容将全部消失)<br>10,"wb+".功能与"w+"同.只是在随后的读写时,可以由位置函数设置读和写的起始位置.<br>最常用的文件使用方式及其含义如下:<br>11,"a+".功能与"a"相同;只是在文件尾部添加新的数据后,可以从头开始读.<br>12,"ab+".功能与"a+"相同;只是在文件尾部添加新数据之后,可以由位置函数设置开始读的起始位置.</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>13.4关闭文件</strong></div>
<div>当文件的读写操作完成之后,使用fclose函数关闭文件.格式如下:<br>fclose(文件指针)<br>如:fclose(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);</div>
<div>&nbsp;</div>
<div><br><strong>13.5调用getc(fgetc)和putc(fputc)函数进行输入和输出</strong><br>1,调用putc(或fputc)函数输出一个字符<br>调用形式为:<br>putc(ch,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>功能是:将字符ch写到文件指针<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>所指的文件中去.当输出成功,putc函数返回所输出的字符;否则,返回一个EOF值.EOF是在stdio.h库函数文件中定义的符号常量,其值等于-1.</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div><strong>13.5调用getc(fgetc)和putc(fputc)函数进行输入和输出</strong><br>例如:把从键盘输入的文本按原样输出到名为file_1.dat文件中,用字符@作为键盘输入结束标志.<br>#include<br>Void main()<br>{<br>FILE *fpout;<br>char ch;<br>if(fpout=fpopen("file_1","w")==NULL)<br>{<br>printf("Cannot open this file!\n");<br>exit(0);<br>}<br>ch=getchar();<br>while(ch!='@')<br>{ fputc(ch,fpout); ch=getchar(); }<br>fclose(fpout);<br>}<br>2.调用getc(或fgetc)函数输入一个字符<br>调用形式为:<br>ch=getc(pf);<br>功能是:从pf指定的文件中读如一个字符,并把它作为函数值返回.<br>例如:把一个已存在磁盘上的file_1.dat文本文件中的内容,原样输出到终端屏幕上.<br>#include<br>void main(){<br>FILE *fpin;<br>char ch;<br>if((fpin=fopen("file_1.dat","r"))==NULL)<br>{ printf("Cann't open this file!\n");exit(0);}<br>ch=fgetc(fpin);<br>while (ch!=EOF)<br>{ putchar(ch); ch=fgetc(fpin);}<br>fclose(fpin);<br>}</div>
<div>&nbsp;</div>
<div><br><strong>13.6判断文件结束函数feof<br></strong>EOF可以作为文本文件的结束 标志,但不能作为二进制文件的结束符.feof函数既可以判断二进制文件,又可以判断文本文件.<br>例:编写程序,用于把一个文本文件(源)复制到另一个文件(目的)中,源文件名和目的文件名由命令行输入.命令形式如下:<br>可执行程序名 源文件名 目的文件名<br>#include <br>void filecopy(FILE* ,FILE *);<br>void main(int argc,char *argv[]){<br>FILE *fpin,*fpout;<br>if(argc==3)<br>{ fpin=fopen(argv[1],"r");<br>fpout=fopen(argv[2],"w");<br>filecopy(fpin,fpout);<br>fclose(fpin);fclose(fpout);<br>}<br>else if(argc&gt;3) <br>printf("The file names too many!!\n";<br>else<br>printf("There are no file names for input or output!!\n );<br>}<br>void filecopy(FILE *fpin,FILE *fpout)<br>{<br>char ch;<br>ch=getc(fpin);<br>while(!feof(fpin))<br>{putc(ch,fpout); ch=getc(fpin);}<br>}</div>
<div>&nbsp;</div>
<div><br><strong>13.7fscanf函数和fprintf函数</strong><br>1,fscanf函数<br>fscanf只能从文本文件中按格式输入,和scanf函数相似,只不过输入的对象是磁盘上文本文件中的数据.调用形式为:<br>fscanf(文件指针,格式控制字符串,输入项表)<br>例如:fscanf(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,"%d%d",&amp;a,&amp;b);<br>fscanf(stdin,"%d%d",&amp;a,&amp;b);<br>等价于scanf("%d%d",&amp;a,&amp;b);<br>3.fprintf函数<br>fprintf函数按格式将内存中的数据转换成对应的字符,并以ASCII代码形式输出到文本文件中.Fprintf函数和printf函数相似,只是将输出的内容按格式存放到磁盘的文本文件中.调用形式如下:<br>fprintf(文件指针,格式控制字符串,输出项表)<br>如:fprintf(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,"%d %d",x,y);<br>以下语句 fprintf(stdout,"%d %d",x,y)</div>
<div>&nbsp;</div>
<div><br><strong>13.8fgets函数和fputs函数<br></strong>1,fgets函数<br>fgets函数用来从文件中读入字符串.调用形式如下:<br>fgets(str,n,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>函数功能是:从<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>所指文件中读入n-1个字符放入str为起始地址的空间内;如果在未读满n-1个字符时,则遇到换行符或一个EOF结束本次读操作,并已str作为函数值返回.<br>13.8fgets函数和fputs函数<br>2,fputs函数<br>fput函数把字符串输出到文件中.函数调用形式如下:<br>fputs(str,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>注意:为了便于读入,在输出字符串时,应当人为的加诸如"\n"这样的字符串.<br></div>
<div>#include &lt;stdio.h&gt;<br>#include &lt;iostream&gt;</div>
<div>int main(int argc, char *argv[])<br>{<br>&nbsp;char arr[10] ;<br>&nbsp;char *ap = "hello!" ;<br>&nbsp;FILE *fp ;<br>&nbsp;if ((fp = fopen("hello.txt", "wt+")) == NULL)<br>&nbsp;{<br>&nbsp;&nbsp;printf("error!") ;<br>&nbsp;&nbsp;exit(1) ;<br>&nbsp;}<br>&nbsp;&nbsp;&nbsp; fputs(ap, fp) ;<br><br>&nbsp;rewind(fp) ; //<br><br>&nbsp;fgets(arr, 10, fp) ;<br>&nbsp;printf("%s\n", arr) ;<br>&nbsp;fclose(fp) ;<br>&nbsp;return 0 ;<br>}</div>
<div><br></div>
<div><br><strong>13.9fread函数和fwrite函数</strong><br>例如有如下结构体:<br>struct st{<br>char num[8];<br>float mk[5];<br>}pers[30];<br>以下循环将把这30个元素中的数据输出到<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>所指文件中.<br>for(i=0;i&lt;30;i++)<br>fwrite(&amp;pers[i],sizeof(struct st),1,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);</div>
<div>&nbsp;</div>
<div><br><strong>13.9fread函数和fwrite函数</strong><br>以下语句从<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>所指的文件中再次将每个学生数据逐个读入到pers数组中.<br>i=0;<br>fread(&amp;pers[i],sizeof(struct st),1,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>while(!feof(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>))<br>{ i++;<br>fread(&amp;pers[i],sizeof(struct st),1,<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>}</div>
<div>&nbsp;</div>
<div><br><strong>13.10文件定位函数</strong><br>1,<a name=0></a><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>函数<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>函数用来移动文件位置指针到指定的位置上,接着的读或写操作将从此位置开始.函数的调用形式如下:<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(pf,offset,origin)<br>pf:文件指针<br>offset:以字节为单位的位移量,为长整形.<br>origin:是起始点,用来指定位移量是以哪个位置为基准的.<br>1,<strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>函数<br>位移量的表示方法<br>标识符 数字 代表的起始点 <br>SEEK_SET 0 文件开始<br><a name=3></a><strong style="COLOR: black; BACKGROUND-COLOR: #ff9999">SEEK_END</strong> 2 文件末尾<br>SEEK_CUR 1 文件当前位置<br>假设pf已指向一个二进制文件,则;<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(pf,3<a name=2></a><strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">0L</strong>,SEEK_SET)<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(pf,-1<strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">0L</strong>*sizeof(int),<strong style="COLOR: black; BACKGROUND-COLOR: #ff9999">SEEK_END</strong>)<br>对于文本文件,位移量必须是0;如:<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(pf,<strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">0L</strong>,SEEK_SET)<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(pf,<strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">0L</strong>,<strong style="COLOR: black; BACKGROUND-COLOR: #ff9999">SEEK_END</strong>)<br>2. ftell函数<br>ftell函数用以获得文件当前位置指针的位置,函数给出当前位置指针相对于文件开头的字节数.如;<br>long t;<br>t=ftell(pf);<br>当函数调用出错时,函数返回-1L.<br>我们可以通过以下方式来测试一个文件的长度:<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,<strong style="COLOR: black; BACKGROUND-COLOR: #99ff99">0L</strong>,<strong style="COLOR: black; BACKGROUND-COLOR: #ff9999">SEEK_END</strong>);<br>t=ftell(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>3.rewind函数<br>调用形式为:<br>rewind(pf);<br>函数没有返回值.函数的功能是使文件的位置指针回到文件的开头.</div>
<div>&nbsp;</div>
<div><br><strong>13.10文件应用</strong><br>在磁盘上的test.txt文件中放有10个不小于2的正整数,用函数调用方式编写程序.要求实现:<br>1,在被调函数prime中,判断和统计10个整数中的素数以及个数.<br>2,在主函数中将全部素数追加到磁盘文件test.txt的尾部,同时输出到屏幕上.<br>#include<br>#include<br>Int prime(int a[],int n)<br>{<br>int I,j,k=0,flag=0;<br>for(i=0;i { for(j=2;j if(a[i]%j==0)<br>{ flag=0; break;}<br>else flag=1;<br>if(flag) <br>{a[k]=a[i];k++;}<br>}<br>return k;<br>}<br>void main(){<br>int n,I,a[10];<br>FILE *<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>;<br><strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>=fopen("test1-2.txt","r+");<br>for(n=0;n&lt;10;n++)<br>fscanf(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,"%d",&amp;a[n]);<br>n=prime(a,n);<br><strong style="COLOR: black; BACKGROUND-COLOR: #ffff66">fseek</strong>(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,o,2);<br>for(i=0;i {printf("%3d",a[i]);<br>fprintf(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>,"%3d",a[i]);<br>}<br>fclose(<strong style="COLOR: black; BACKGROUND-COLOR: #a0ffff">fp</strong>);<br>}</div>
</div>
<img src ="http://www.cppblog.com/liu1061/aggbug/38441.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-13 17:09 <a href="http://www.cppblog.com/liu1061/articles/38441.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>learn file op!</title><link>http://www.cppblog.com/liu1061/articles/38316.html</link><dc:creator>T.S Liu</dc:creator><author>T.S Liu</author><pubDate>Wed, 12 Dec 2007 07:36:00 GMT</pubDate><guid>http://www.cppblog.com/liu1061/articles/38316.html</guid><wfw:comment>http://www.cppblog.com/liu1061/comments/38316.html</wfw:comment><comments>http://www.cppblog.com/liu1061/articles/38316.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/liu1061/comments/commentRss/38316.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/liu1061/services/trackbacks/38316.html</trackback:ping><description><![CDATA[<p>&nbsp;</p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_1_102_Open_Image onclick="this.style.display='none'; Codehighlighter1_1_102_Open_Text.style.display='none'; Codehighlighter1_1_102_Closed_Image.style.display='inline'; Codehighlighter1_1_102_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_1_102_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1_102_Closed_Text.style.display='none'; Codehighlighter1_1_102_Open_Image.style.display='inline'; Codehighlighter1_1_102_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_1_102_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</span><span id=Codehighlighter1_1_102_Open_Text><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;函数说明:<br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1)、srcFile为源文件，loc为要检查的位置，databit为要检查的数据位，data为需要排除的数据位<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2)、成功返回1，失败返回0<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top></span><span style="COLOR: #008000">*/</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;updateContent(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">srcFile,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;loc,</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;databit,&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;data)<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img id=Codehighlighter1_191_2192_Open_Image onclick="this.style.display='none'; Codehighlighter1_191_2192_Open_Text.style.display='none'; Codehighlighter1_191_2192_Closed_Image.style.display='inline'; Codehighlighter1_191_2192_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align=top><img id=Codehighlighter1_191_2192_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_191_2192_Closed_Text.style.display='none'; Codehighlighter1_191_2192_Open_Image.style.display='inline'; Codehighlighter1_191_2192_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif" align=top></span><span id=Codehighlighter1_191_2192_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_191_2192_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;FILE&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">fsrc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL,</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">ftmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;BUFF[</span><span style="COLOR: #000000">256</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">11</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;tempfile[]</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">tempfileXXXXXX</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">pbitdata</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">delim</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">14</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;assert((NULL</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">srcFile)&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;(loc</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;(databit</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;databit</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;(data</span><span style="COLOR: #000000">&gt;=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">&amp;&amp;</span><span style="COLOR: #000000">&nbsp;data</span><span style="COLOR: #000000">&lt;=</span><span style="COLOR: #000000">9</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">16</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">(ftmp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fopen(mktemp(tempfile),</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)))<br></span><span style="COLOR: #008080">18</span><span style="COLOR: #000000"><img id=Codehighlighter1_486_656_Open_Image onclick="this.style.display='none'; Codehighlighter1_486_656_Open_Text.style.display='none'; Codehighlighter1_486_656_Closed_Image.style.display='inline'; Codehighlighter1_486_656_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_486_656_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_486_656_Closed_Text.style.display='none'; Codehighlighter1_486_656_Open_Image.style.display='inline'; Codehighlighter1_486_656_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_486_656_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_486_656_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果打开临时文件失败</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">19</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;%s:%d&gt;open(or&nbsp;create)&nbsp;file&nbsp;[%s]&nbsp;failed!\nERROR!!!&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__FILE__,__LINE__,tempfile,strerror(errno));<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">(fsrc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">fopen(srcFile,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">r</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)))<br></span><span style="COLOR: #008080">26</span><span style="COLOR: #000000"><img id=Codehighlighter1_704_910_Open_Image onclick="this.style.display='none'; Codehighlighter1_704_910_Open_Text.style.display='none'; Codehighlighter1_704_910_Closed_Image.style.display='inline'; Codehighlighter1_704_910_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_704_910_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_704_910_Closed_Text.style.display='none'; Codehighlighter1_704_910_Open_Image.style.display='inline'; Codehighlighter1_704_910_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_704_910_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_704_910_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果打开源文件失败</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">27</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;%s:%d&gt;open&nbsp;file&nbsp;[%s]&nbsp;failed!\nERROR!!!&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,<br></span><span style="COLOR: #008080">28</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__FILE__,__LINE__,srcFile,strerror(errno));<br></span><span style="COLOR: #008080">29</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">30</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose(ftmp);<br></span><span style="COLOR: #008080">31</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remove(tempfile);<br></span><span style="COLOR: #008080">32</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">33</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">34</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">35</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">36</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">feof(fsrc))<br></span><span style="COLOR: #008080">37</span><span style="COLOR: #000000"><img id=Codehighlighter1_941_1704_Open_Image onclick="this.style.display='none'; Codehighlighter1_941_1704_Open_Text.style.display='none'; Codehighlighter1_941_1704_Closed_Image.style.display='inline'; Codehighlighter1_941_1704_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_941_1704_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_941_1704_Closed_Text.style.display='none'; Codehighlighter1_941_1704_Open_Image.style.display='inline'; Codehighlighter1_941_1704_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_941_1704_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_941_1704_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">38</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">tmpStr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br></span><span style="COLOR: #008080">39</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;curNum</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">40</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">41</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(BUFF,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(BUFF));<br></span><span style="COLOR: #008080">42</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">fgets(BUFF,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(BUFF),fsrc))<br></span><span style="COLOR: #008080">43</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">44</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpStr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strdup(BUFF);<br></span><span style="COLOR: #008080">45</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pbitdata</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strtok(BUFF,delim);<br></span><span style="COLOR: #008080">46</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">pbitdata)<br></span><span style="COLOR: #008080">47</span><span style="COLOR: #000000"><img id=Codehighlighter1_1206_1546_Open_Image onclick="this.style.display='none'; Codehighlighter1_1206_1546_Open_Text.style.display='none'; Codehighlighter1_1206_1546_Closed_Image.style.display='inline'; Codehighlighter1_1206_1546_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1206_1546_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1206_1546_Closed_Text.style.display='none'; Codehighlighter1_1206_1546_Open_Image.style.display='inline'; Codehighlighter1_1206_1546_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1206_1546_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1206_1546_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">分割字符串</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">48</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(curNum</span><span style="COLOR: #000000">++==</span><span style="COLOR: #000000">loc)<br></span><span style="COLOR: #008080">49</span><span style="COLOR: #000000"><img id=Codehighlighter1_1258_1495_Open_Image onclick="this.style.display='none'; Codehighlighter1_1258_1495_Open_Text.style.display='none'; Codehighlighter1_1258_1495_Closed_Image.style.display='inline'; Codehighlighter1_1258_1495_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1258_1495_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1258_1495_Closed_Text.style.display='none'; Codehighlighter1_1258_1495_Open_Image.style.display='inline'; Codehighlighter1_1258_1495_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1258_1495_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1258_1495_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">已找到指定位置的数据<br></span><span style="COLOR: #008080">50</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果不符合要求,则保留,否则删除</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">51</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(data</span><span style="COLOR: #000000">!=*</span><span style="COLOR: #000000">(pbitdata</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">databit</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">48</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">52</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fputs(tmpStr,ftmp);<br></span><span style="COLOR: #008080">53</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">54</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(tmpStr);<br></span><span style="COLOR: #008080">55</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpStr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br></span><span style="COLOR: #008080">56</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">57</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">58</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">59</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pbitdata</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">strtok(NULL,delim);<br></span><span style="COLOR: #008080">60</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">61</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">如果没有找到指定的位置</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">62</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">tmpStr)<br></span><span style="COLOR: #008080">63</span><span style="COLOR: #000000"><img id=Codehighlighter1_1604_1698_Open_Image onclick="this.style.display='none'; Codehighlighter1_1604_1698_Open_Text.style.display='none'; Codehighlighter1_1604_1698_Closed_Image.style.display='inline'; Codehighlighter1_1604_1698_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1604_1698_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1604_1698_Closed_Text.style.display='none'; Codehighlighter1_1604_1698_Open_Image.style.display='inline'; Codehighlighter1_1604_1698_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1604_1698_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1604_1698_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">64</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fputs(tmpStr,ftmp);<br></span><span style="COLOR: #008080">65</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">66</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(tmpStr);<br></span><span style="COLOR: #008080">67</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmpStr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">NULL;<br></span><span style="COLOR: #008080">68</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">69</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">70</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">71</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">(fsrc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">freopen(srcFile,</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">w</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,fsrc)))<br></span><span style="COLOR: #008080">72</span><span style="COLOR: #000000"><img id=Codehighlighter1_1759_1918_Open_Image onclick="this.style.display='none'; Codehighlighter1_1759_1918_Open_Text.style.display='none'; Codehighlighter1_1759_1918_Closed_Image.style.display='inline'; Codehighlighter1_1759_1918_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1759_1918_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1759_1918_Closed_Text.style.display='none'; Codehighlighter1_1759_1918_Open_Image.style.display='inline'; Codehighlighter1_1759_1918_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1759_1918_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1759_1918_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">重新打开源文件失败</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">73</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;%s:%d&gt;reopen&nbsp;file&nbsp;[%s]&nbsp;failed!\nERROR!!!&nbsp;%s\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">,<br></span><span style="COLOR: #008080">74</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;__FILE__,__LINE__,srcFile,strerror(errno));<br></span><span style="COLOR: #008080">75</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">76</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">77</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">78</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;rewind(ftmp);<br></span><span style="COLOR: #008080">79</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">80</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">&nbsp;(</span><span style="COLOR: #000000">!</span><span style="COLOR: #000000">feof(ftmp))<br></span><span style="COLOR: #008080">81</span><span style="COLOR: #000000"><img id=Codehighlighter1_1967_2117_Open_Image onclick="this.style.display='none'; Codehighlighter1_1967_2117_Open_Text.style.display='none'; Codehighlighter1_1967_2117_Closed_Image.style.display='inline'; Codehighlighter1_1967_2117_Closed_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><img id=Codehighlighter1_1967_2117_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1967_2117_Closed_Text.style.display='none'; Codehighlighter1_1967_2117_Open_Image.style.display='inline'; Codehighlighter1_1967_2117_Open_Text.style.display='inline';" src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id=Codehighlighter1_1967_2117_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_1967_2117_Open_Text><span style="COLOR: #000000">{</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">更新至原来的文件</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">82</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memset(BUFF,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(BUFF));<br></span><span style="COLOR: #008080">83</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(NULL</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">fgets(BUFF,</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(BUFF),ftmp))<br></span><span style="COLOR: #008080">84</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">85</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fputs(BUFF,fsrc);<br></span><span style="COLOR: #008080">86</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">87</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;fclose(fsrc);<br></span><span style="COLOR: #008080">88</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;fclose(ftmp);<br></span><span style="COLOR: #008080">89</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;remove(tempfile);<br></span><span style="COLOR: #008080">90</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top><br></span><span style="COLOR: #008080">91</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">92</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">93</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top><br></span><span style="COLOR: #008080">94</span><span style="COLOR: #000000"><img src="http://www.cppblog.com/Images/OutliningIndicators/None.gif" align=top></span></div>
<img src ="http://www.cppblog.com/liu1061/aggbug/38316.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/liu1061/" target="_blank">T.S Liu</a> 2007-12-12 15:36 <a href="http://www.cppblog.com/liu1061/articles/38316.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>