<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Puzzle</title>
</head>

<body>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000" size="6"><b>&nbsp;关于feof()的bug??</b></font>  
</p>
<p>这是我针对feof()错误有专门写的一个简单的测试.  
</p>
<p>1.写5条记录到文件，每条一个字串，一个整数。  
</p>
<p>2。从文件里读出，结果feof()无法识别。  
</p>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;</pre>
<pre>using namespace std;</pre>
<pre>int main()
{
	char *str[5] = {&quot;first&quot;,&quot;second&quot;, &quot;third&quot;,&quot;fourth&quot;,&quot;fifty&quot;};
	int  index[5] = {0, 1, 2, 3, 4};	</pre>
<pre>	FILE* stream;</pre>
<pre>	//write 5 record in file, each record is a string and int.
	stream = fopen(&quot;c:\\nickfile.txt&quot;, &quot;w+b&quot;);
	for (int i=0; i&lt;5; i++)
	{
		fwrite((void*)str[i], sizeof(char), strlen(str[i]), stream);
		fputc('\0', stream);   //use null for end
		fwrite((void*)(&amp;index[i]), sizeof(int), 1, stream);
	}</pre>
<pre>	FILE* file;</pre>
<pre>	fclose(stream);
	char buffer[20], ch;
	char* ptr = buffer;
	int number;
		</pre>
<pre>	file = fopen(&quot;c:\\nickfile.txt&quot;, &quot;r+b&quot;);
	while(!feof(file))   //this is not functioning well!!
	{
		ptr = buffer;
		do
		{
			ch = fgetc(file);
			*ptr = ch;
			ptr++;
		}
		while (ch!='\0');
		cout&lt;&lt;&quot;str is:&quot;&lt;&lt;buffer&lt;&lt;endl;
		fread((void*)(&amp;number), sizeof(int), 1, file);
		cout&lt;&lt;&quot;index is:&quot;&lt;&lt;number&lt;&lt;endl;
	}
	</pre>
<pre>	return 0;
}</pre>
<pre><a href="picture/filestream.JPG"><img border="0" src="picture/filestream.JPG" width="693" height="433"></a>
</pre>
<pre><font size="3">3。 我问了学校里c++的老师，她的意思这仿佛是一个人人皆知的bug.她说如果用fstream的类，就不会有问题，我就又写</font></pre>
<pre><font size="3">了一个测试。如下：</font></pre>
<pre><font size="3">4。数据之间用换行符隔开，这应该是ofstream的默认吧。结果，ifstream.eof()函数正常。</font></pre>
<pre>　</pre>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;</pre>
<pre>using namespace std;</pre>
<pre>int main()
{
	char *str[5] = {&quot;first&quot;,&quot;second&quot;, &quot;third&quot;,&quot;fourth&quot;,&quot;fifty&quot;};
	int  index[5] = {0, 1, 2, 3, 4};	</pre>
<pre>	ofstream out;
	out.open(&quot;c:\\nickfile.txt&quot;);
	for (int i=0; i&lt; 5; i++)
	{
		out&lt;&lt;'\n'&lt;&lt;str[i];
		out&lt;&lt;'\n'&lt;&lt;index[i];
	}</pre>
<pre>	out.close();</pre>
<pre>	char buffer[20];
	int number;
	ifstream in;
	in.open(&quot;c:\\nickfile.txt&quot;);
	while (!in.eof())
	{
		in&gt;&gt;buffer;
		cout&lt;&lt;&quot;str is:&quot;&lt;&lt;buffer&lt;&lt;endl;
		in&gt;&gt;number;
		cout&lt;&lt;&quot;number is:&quot;&lt;&lt;number&lt;&lt;endl;
	}	</pre>
<pre>	return 0;
}</pre>
<pre><img border="0" src="picture/fstream.JPG" width="669" height="437"></pre>
<pre>　</pre>
<pre>
</pre>
<pre>　</pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     
&nbsp;&nbsp;&nbsp; <a href="Precedence.htm">     







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="logic.htm"><img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">         


</a>         


</p>

</body>

</html>
