﻿<?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++博客-Apollo Fang's Blog-随笔分类-C/C++</title><link>http://www.cppblog.com/scorpio/category/17989.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 04 Nov 2012 15:57:48 GMT</lastBuildDate><pubDate>Sun, 04 Nov 2012 15:57:48 GMT</pubDate><ttl>60</ttl><item><title>文件模式</title><link>http://www.cppblog.com/scorpio/archive/2012/10/31/194099.html</link><dc:creator>Apollo Fang</dc:creator><author>Apollo Fang</author><pubDate>Wed, 31 Oct 2012 08:40:00 GMT</pubDate><guid>http://www.cppblog.com/scorpio/archive/2012/10/31/194099.html</guid><wfw:comment>http://www.cppblog.com/scorpio/comments/194099.html</wfw:comment><comments>http://www.cppblog.com/scorpio/archive/2012/10/31/194099.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/scorpio/comments/commentRss/194099.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/scorpio/services/trackbacks/194099.html</trackback:ping><description><![CDATA[<div>  <p>&nbsp;</p>  <p style="background:#B3B3B3"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文件</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:&quot;Courier New&quot;">C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">将文件看做是连续字节序列</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">其中每个字节都可以单独读取</span><span style="font-family: &quot;Courier New&quot;">.</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">这个与</span><span style="font-family:&quot;Courier New&quot;">UNIX</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">环境中的文件结构是一致的</span><span style="font-family: &quot;Courier New&quot;">.</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">但</span><span style="font-family:&quot;Courier New&quot;">ANSI C</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">为了与其他的</span><span style="font-family:&quot;Courier New&quot;">OS</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">环境兼容</span><span style="font-family:&quot;Courier New&quot;">(</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">比如</span><span style="font-family:&quot;Courier New&quot;">Windows),</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">提供了两种文件视图</span><span style="font-family:&quot;Courier New&quot;">: </span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文本视图和二进制视图</span><span style="font-family: &quot;Courier New&quot;">.</span></p>  <p style="background:#B3B3B3"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文本视图和二进制视图</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">二进制视图中</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文件中的每个字节都可以为程序访问</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">但文本视图中</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序中看到的内容与文件的内容可能不同</span><span style="font-family:&quot;Courier New&quot;">.</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">例如</span><span style="font-family:&quot;Courier New&quot;">MS-DOS</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文本文件用</span><span style="font-family:&quot;Courier New&quot;">"\r\n"</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">来表示行尾</span><span style="font-family:&quot;Courier New&quot;">; Macintosh</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文本文件中用</span><span style="font-family:&quot;Courier New&quot;">"\r"</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">表示行尾</span><span style="font-family:&quot;Courier New&quot;">; C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序使用</span><span style="font-family:&quot;Courier New&quot;">"\n"</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">表示行尾</span><span style="font-family:&quot;Courier New&quot;">. So,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">如果</span><span style="font-family:&quot;Courier New&quot;">C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序以文本视图模式处理一个</span><span style="font-family: &quot;Courier New&quot;">MS-DOS</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文本文件</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">在读取文件时</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">就会将</span><span style="font-family:&quot;Courier New&quot;">"\r\n"</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">转换为</span><span style="font-family:&quot;Courier New&quot;">"\n",</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">在写入文件时</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">就会将</span><span style="font-family:&quot;Courier New&quot;">"\n"</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">转换为</span><span style="font-family:&quot;Courier New&quot;">"\r\n". Macintosh</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">同理</span><span style="font-family:&quot;Courier New&quot;">.</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">说白了</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">两种视图的实现是一样的</span><span style="font-family: &quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">只是在处理行尾或处理文件结尾时有点不同而已</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">采用某种视图打开文件时</span><span style="font-family: &quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">注意一下就是了</span><span style="font-family:&quot;Courier New&quot;">.</span></p>  <p style="background:#B3B3B3"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文件结尾和换行</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文件读取数据的程序需要在达到文件结尾时停止</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">当到达文件结尾时</span><span style="font-family: &quot;Courier New&quot;">,"getc()"</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">函数会返回一个特殊值</span><span style="font-family: &quot;Courier New&quot;">EOF.</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">所以</span><span style="font-family:&quot;Courier New&quot;">C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序只有在读取超出文件结尾后</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">才会发现文件的结尾</span><span style="font-family: &quot;Courier New&quot;">.</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">为了避免读取空文件带来的问题</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">应该对文件输入使用入口条件循环</span><span style="font-family:&quot;Courier New&quot;">(</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">使用</span><span style="font-family:&quot;Courier New&quot;">while</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">或</span><span style="font-family:&quot;Courier New&quot;">for,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">避免使用</span><span style="font-family:&quot;Courier New&quot;">do...while)</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">如下设计</span><span style="font-family:&quot;Courier New&quot;">:<br /></span></p><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff; font-family: Courier; ">int</span><span style="font-family: Courier; ">&nbsp;ch;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier; ">//</span><span style="color: #008000; font-family: Courier; ">&nbsp;Watching&nbsp;EOF</span><span style="color: #008000; "><br /></span><span style="font-family: Courier; ">FILE*&nbsp;fp;</span><br /><span style="font-family: Courier; ">fp&nbsp;=&nbsp;fopen&nbsp;("_FileName",&nbsp;"_Mode");</span><br /><span style="font-family: Courier; ">ch&nbsp;=&nbsp;getc&nbsp;(fp);</span><br />&nbsp;<br /><span style="color: #0000ff; font-family: Courier; ">while</span><span style="font-family: Courier; ">&nbsp;(ch&nbsp;!=&nbsp;EOF)</span><br /><span style="font-family: Courier; ">{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;putchar&nbsp;(ch);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;=&nbsp;getc&nbsp;(fp);</span><br /><span style="font-family: Courier; ">}</span></div></div><div>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">上面的例子可以看做是一个框架</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">来进行文件结尾的判断</span><span style="font-family: &quot;Courier New&quot;">.</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">但</span><span style="font-family:&quot;Courier New&quot;">ANSI C</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">的两种模式</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">对于文件结尾有不同的解释</span><span style="font-family: &quot;Courier New&quot;">.</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">如果文件以文本模式打开</span><span style="font-family:&quot;Courier New&quot;">,C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">可以认出</span><span style="font-family:&quot;Courier New&quot;">EOF</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">标志文件结尾</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">如果以二进制模式打开</span><span style="font-family: &quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">就会把</span><span style="font-family:&quot;Courier New&quot;">EOF</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">当做是文件中的一个字符</span><span style="font-family: &quot;Courier New&quot;">.</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">真正的文件结尾还在后面</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">文件的结尾</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">可能紧跟着</span><span style="font-family:&quot;Courier New&quot;">EOF,</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">当然</span><span style="font-family:&quot;Courier New&quot;">,</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">也可能用空字符填充文件使其大小为</span><span style="font-family:&quot;Courier New&quot;">256(</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">或其他数</span><span style="font-family:&quot;Courier New&quot;">)</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">的倍数</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">在</span><span style="font-family:&quot;Courier New&quot;">DOS</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">下不打印空字符</span><span style="font-family:&quot;Courier New&quot;">.</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序中包含了防止程序打印</span><span style="font-family: &quot;Courier New&quot;">EOF</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">字符的代码</span><span style="font-family:&quot;Courier New&quot;">.</span></p>  <p style="text-indent:21.0pt;"><span style="font-family:&quot;Courier New&quot;">MS-DOS</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">的文本文件用二进制模式和文本模式打开</span><span style="font-family:&quot;Courier New&quot;">,C</span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">程序将看到下面的内容</span><span style="font-family: &quot;Courier New&quot;">:</span></p>  <span style="font-size: 10pt; ">  </span><table border="1" cellspacing="0" cellpadding="0" style="margin-left: 5.4pt; border-collapse: collapse; border: none; "><tbody><tr>   <td width="352" valign="top" style="width: 263.65pt; border: 1pt solid windowtext; padding: 0in 5.4pt; ">   <p><span style="font-family: 宋体; font-size: 10pt; ">二进制模式打开文件</span><span style="font-family: 'Courier New'; font-size: 10pt; ">C</span><span style="font-family: 宋体; font-size: 10pt; ">程序看到的内容</span><span style="font-family: 'Courier New'; font-size: 10pt; ">:</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line1\r\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line2\r\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line3\r\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">^Z</span></p>   </td>   <td width="350" valign="top" style="width: 262.85pt; border-style: solid solid solid none; border-top-color: windowtext; border-right-color: windowtext; border-bottom-color: windowtext; border-top-width: 1pt; border-right-width: 1pt; border-bottom-width: 1pt; padding: 0in 5.4pt; ">   <p><span style="font-family: 宋体; font-size: 10pt; ">文本模式打开文件</span><span style="font-family: 'Courier New'; font-size: 10pt; ">C</span><span style="font-family: 宋体; font-size: 10pt; ">程序看到的内容</span><span style="font-family: 'Courier New'; font-size: 10pt; ">:</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line1\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line2\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">line3\n</span></p>   <p><span style="font-family: 'Courier New'; font-size: 10pt; ">^Z</span></p></td></tr></tbody></table></div>  <p style="background:#B3B3B3"><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">例子</span><span style="font-family:&quot;Courier New&quot;"> : </span><span style="font-family: 宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;">逆序输出一个文件内容</span></p>  <div style="background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="font-family: Courier; ">#include&nbsp;</span><span style="font-family: Courier; ">&lt;</span><span style="font-family: Courier; ">stdio.h</span><span style="font-family: Courier; ">&gt;</span><br /><span style="font-family: Courier; ">#include&nbsp;</span><span style="font-family: Courier; ">&lt;</span><span style="font-family: Courier; ">stdlib.h</span><span style="font-family: Courier; ">&gt;</span><br /><span style="color: #0000ff; font-family: Courier; ">#define</span><span style="font-family: Courier; ">&nbsp;MAC</span><br /><span style="color: #0000ff; font-family: Courier; ">#define</span><span style="font-family: Courier; ">&nbsp;CNTL_Z&nbsp;'\032'&nbsp;/*&nbsp;DOS&nbsp;Text&nbsp;File&nbsp;End&nbsp;Of&nbsp;File&nbsp;Flag&nbsp;*/</span><br /><span style="color: #0000ff; font-family: Courier; ">#define</span><span style="font-family: Courier; ">&nbsp;SLEN&nbsp;&nbsp;&nbsp;50</span><br /><span style="color: #0000ff; font-family: Courier; ">#define</span><span style="font-family: Courier; ">&nbsp;PATH&nbsp;&nbsp;</span><br /><span style="color: #0000ff; font-family: Courier; ">int</span><span style="font-family: Courier; ">&nbsp;main&nbsp;(</span><span style="color: #0000ff; font-family: Courier; ">void</span><span style="font-family: Courier; ">)</span><br /><span style="font-family: Courier; ">{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">char</span><span style="font-family: Courier; ">&nbsp;file[SLEN];</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">char</span><span style="font-family: Courier; ">&nbsp;ch;</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;FILE</span><span style="font-family: Courier; ">*</span><span style="font-family: Courier; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Courier; ">fp;</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">long</span><span style="font-family: Courier; ">&nbsp;count;</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">long</span><span style="font-family: Courier; ">&nbsp;last;</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;puts&nbsp;(</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">Enter&nbsp;Name&nbsp;Of&nbsp;File&nbsp;To&nbsp;Be&nbsp;Processed&nbsp;:&nbsp;</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;gets&nbsp;(file);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">if</span><span style="font-family: Courier; ">&nbsp;((fp&nbsp;</span><span style="font-family: Courier; ">=</span><span style="font-family: Courier; ">&nbsp;fopen&nbsp;(file,&nbsp;</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">rb</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">))&nbsp;</span><span style="font-family: Courier; ">==</span><span style="font-family: Courier; ">&nbsp;NULL)</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf&nbsp;(</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">Reverse&nbsp;Can't&nbsp;Open&nbsp;%s&nbsp;.\n</span><span style="font-family: Courier; ">"</span><span style="font-family: Courier; ">,&nbsp;file);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit&nbsp;(</span><span style="font-family: Courier; ">1</span><span style="font-family: Courier; ">);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;fseek&nbsp;(fp,&nbsp;</span><span style="font-family: Courier; ">0L</span><span style="font-family: Courier; ">,&nbsp;SEEK_END);&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier; ">/*</span><span style="color: #008000; font-family: Courier; ">&nbsp;Locate&nbsp;To&nbsp;End&nbsp;Of&nbsp;File&nbsp;</span><span style="color: #008000; font-family: Courier; ">*/</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;last&nbsp;</span><span style="font-family: Courier; ">=</span><span style="font-family: Courier; ">&nbsp;ftell&nbsp;(fp);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">for</span><span style="font-family: Courier; ">&nbsp;(count&nbsp;</span><span style="font-family: Courier; ">=</span>&nbsp;<span style="font-family: Courier; ">1L</span><span style="font-family: Courier; ">;&nbsp;count&nbsp;</span><span style="font-family: Courier; ">&lt;=</span><span style="font-family: Courier; ">&nbsp;last;&nbsp;count</span><span style="font-family: Courier; ">++</span><span style="font-family: Courier; ">)</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fseek&nbsp;(fp,&nbsp;</span><span style="font-family: Courier; ">-</span><span style="font-family: Courier; ">count,&nbsp;SEEK_END);&nbsp;&nbsp;</span><span style="color: #008000; font-family: Courier; ">/*</span><span style="color: #008000; font-family: Courier; ">&nbsp;Back&nbsp;1&nbsp;Byte&nbsp;</span><span style="color: #008000; font-family: Courier; ">*/</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;</span><span style="font-family: Courier; ">=</span><span style="font-family: Courier; ">&nbsp;getc&nbsp;(fp);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">if</span><span style="font-family: Courier; ">&nbsp;(ch&nbsp;</span><span style="font-family: Courier; ">!=</span><span style="font-family: Courier; ">&nbsp;CNTL_Z&nbsp;</span><span style="font-family: Courier; ">&amp;&amp;</span><span style="font-family: Courier; ">&nbsp;ch&nbsp;</span><span style="font-family: Courier; ">!=</span>&nbsp;<span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">\r</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">)</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar&nbsp;(ch);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="color: #0000ff; font-family: Courier; ">#if</span><span style="font-family: Courier; ">&nbsp;defined&nbsp;MAC&nbsp;||&nbsp;defined&nbsp;WIN32</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; font-family: Courier; ">/*</span><span style="color: #008000; font-family: Courier; ">&nbsp;Macintosh&nbsp;||&nbsp;Windows&nbsp;</span><span style="color: #008000; font-family: Courier; ">*/</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">if</span><span style="font-family: Courier; ">&nbsp;(ch&nbsp;</span><span style="font-family: Courier; ">==</span>&nbsp;<span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">\r</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">)</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar&nbsp;(</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">\n</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">else</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar&nbsp;(ch);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="color: #0000ff; font-family: Courier; ">#endif</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;putchar&nbsp;(</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">\n</span><span style="font-family: Courier; ">'</span><span style="font-family: Courier; ">);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose&nbsp;(fp);</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff; font-family: Courier; ">return</span>&nbsp;<span style="font-family: Courier; ">0</span><span style="font-family: Courier; ">;</span><br /><span style="font-family: Courier; ">&nbsp;&nbsp;&nbsp;&nbsp;}</span><br /><span style="font-family: Courier; ">}</span></div><br /><br /><br /><br /><img src ="http://www.cppblog.com/scorpio/aggbug/194099.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/scorpio/" target="_blank">Apollo Fang</a> 2012-10-31 16:40 <a href="http://www.cppblog.com/scorpio/archive/2012/10/31/194099.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>About C/C++ Data Type</title><link>http://www.cppblog.com/scorpio/archive/2011/10/25/159060.html</link><dc:creator>Apollo Fang</dc:creator><author>Apollo Fang</author><pubDate>Tue, 25 Oct 2011 09:28:00 GMT</pubDate><guid>http://www.cppblog.com/scorpio/archive/2011/10/25/159060.html</guid><wfw:comment>http://www.cppblog.com/scorpio/comments/159060.html</wfw:comment><comments>http://www.cppblog.com/scorpio/archive/2011/10/25/159060.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/scorpio/comments/commentRss/159060.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/scorpio/services/trackbacks/159060.html</trackback:ping><description><![CDATA[<div><span style="font-size: 11pt; font-family: 'Lucida Console'; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; background-color: transparent; "> &nbsp;&nbsp;&nbsp;1) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> Integer</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;2) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int*</span>&nbsp;&nbsp;<span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">A pointer point to an Integer</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;3) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int**</span><span style="font-size: 11pt; font-family: Consolas; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "><span style="white-space: pre; ">&nbsp;</span></span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">A pointer point to a pointer what point to an Integer</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;4) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int* arr[N]</span><span style="font-size: 11pt; font-family: Consolas; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "><span style="white-space: pre; ">&nbsp;</span></span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">An array that each element is a pointer point to an Integer</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;5) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int(*arr)[N]</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> A pointer point to an array that its each element is an I</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">nteger and it have N elements.</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;6) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int (*fun)(int)</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> A pointer point to a function what with an integer parameter a</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">nd with an integer returning.</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;7) </span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">int (*fun[N])(int)</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> An array that have N elements and each element is a pointer p</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; ">oint to a point that point to a type of function that have an</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> integer parameter and with an integer returning.</span><br /><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;Maybe understand (4) type just like this :</span><br /><span style="font-size: 11pt; font-family: Consolas; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; vertical-align: baseline; white-space: pre-wrap; "><u>int*[N]</u> arr;<br />    It means "arr"'s type is int*[]. And how to catch the meaning of <u>int*[]</u>? At first it's an array, and each element of this array is a pointer point to an integer.<br /></span><span style="font-size: 11pt; font-family: 'Lucida Console'; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; background-color: transparent; "> &nbsp;&nbsp;&nbsp;And can understand (5) type just like this :<br /></span><span style="font-size: 11pt; font-family: 'Lucida Console'; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; background-color: transparent; ">    int[N]* arr;<br /></span><span style="font-size: 11pt; font-family: 'Lucida Console'; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; background-color: transparent; ">    It means "arr"'s type is int[]*. That's to show that this type is a pointer and this pointer point to an integer array its size is N.<br /></span><span style="font-size: 11pt; font-family: 'Lucida Console'; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; background-color: transparent; "> &nbsp;&nbsp;&nbsp;It means this variable value named "fun" and its type<br /></span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> &nbsp;&nbsp;&nbsp;Well, the above are so complex, and we should follow the </span><span style="font-size: 11pt; font-family: 'Lucida Console'; color: #ff0000; background-color: transparent; font-weight: bold; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">KISS</span><span style="font-size: 11pt; font-family: 'Lucida Console'; background-color: transparent; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; "> principle that is </span><span style="font-size: 11pt; font-family: 'Lucida Console'; color: #ff0000; background-color: transparent; font-weight: bold; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">Keep It Simple</span><span style="font-size: 11pt; font-family: 'Lucida Console'; color: #ff0000; background-color: transparent; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">, </span><span style="font-size: 11pt; font-family: 'Lucida Console'; color: #ff0000; background-color: transparent; font-weight: bold; text-decoration: underline; vertical-align: baseline; white-space: pre-wrap; ">Stupid</span><span style="vertical-align: baseline; background-color: transparent; "><font class="Apple-style-span" face="Consolas"><span class="Apple-style-span" style="font-size: 11pt; white-space: pre-wrap; font-family: 'Lucida Console'; ">.</span></font><br /><font class="Apple-style-span" face="Consolas"><span class="Apple-style-span" style="font-size: 11pt; white-space: pre-wrap; font-family: 'Lucida Console'; ">    Did you catch it ??</span></font><br /><br /><br /><br /><div style="text-align: right;"><font class="Apple-style-span" face="Consolas"><span class="Apple-style-span" style="font-size: 15px; white-space: pre-wrap; font-family: 'Lucida Console'; ">Tuesday, October 25, 2011</span></font></div></span></div><img src ="http://www.cppblog.com/scorpio/aggbug/159060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/scorpio/" target="_blank">Apollo Fang</a> 2011-10-25 17:28 <a href="http://www.cppblog.com/scorpio/archive/2011/10/25/159060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>