<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[C/C++文件的操作（fread() fwrite()）]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Wed, 07 Nov 2007 09:18:13 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	复习一下文件的操作，很多时候都用的上。<br/><br/>fread函数和fwrite函数<br/><br/>1.函数功能<br/><br/> &nbsp;用来读写一个数据块。<br/><br/>2.一般调用形式<br/><br/> &nbsp;fread(buffer,size,count,fp);<br/><br/> &nbsp;fwrite(buffer,size,count,fp);<br/><br/>3.说明<br/><br/> &nbsp;（1）buffer：是一个指针，对fread来说，它是读入数据的存放地址。对fwrite来说，是要输出数据的地址。<br/><br/> &nbsp;（2）size：要读写的字节数；<br/><br/> &nbsp;（3）count:要进行读写多少个size字节的数据项；<br/><br/> &nbsp;（4）fp:文件型指针。<br/><br/> 注意：1 完成次写操(fwrite())作后必须关闭流(fclose());<br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 完成一次读操作(fread())后，如果没有关闭流(fclose()),则指针(FILE * fp)自动向后移动前一次读写的长度，不关闭流继续下一次读操作则接着上次的输出继续输出;<br/><br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 fprintf() ： 按格式输入到流，其原型是int fprintf(FILE *stream, const char *format[, argument, ...]);其用法和printf()相同，不过不是写到控制台，而是写到流罢了。注意的是返回值为此次操作写入到文件的字节数。如int c = fprintf(fp, &quot;%s %s %d %f&quot;, str1,str2, a, b) ;str1：10字节；str2： 10字节；a：2字节；b：8字节，c为33，因为写入时不同的数据间自动加入一个空格。<br/><br/>文件使用之后一定要关闭,否则将不能正确显示内容.fwrite:读入两个学生信息然后用fwrite存入文件<br/><br/>fread:用fread从文件中读出学生信息。<br/><br/> <br/><br/>fwrite.c<br/><br/>#include &lt;stdio.h&gt;<br/>#define SIZE 2<br/>struct student_type<br/>{<br/> char name[10];<br/> int num;<br/> int age;<br/> char addr[10];<br/>}stud[SIZE];<br/>void save()<br/>{<br/> FILE *fp;<br/> int i;<br/> if((fp=fopen(&quot;stu_list&quot;,&quot;wb&quot;))==NULL)<br/> {<br/> &nbsp;printf(&quot;cant open the file&quot;);<br/> &nbsp;exit(0);<br/> }<br/> for(i=0;i&lt;SIZE;i++)<br/> {<br/> &nbsp; if(fwrite(&amp;stud[i],sizeof(struct student_type),1,fp)!=1)<br/> &nbsp; &nbsp;printf(&quot;file write error&#92;n&quot;);<br/> }<br/> fclose(fp);<br/>}<br/>main()<br/>{<br/> int i;<br/> for(i=0;i&lt;SIZE;i++)<br/> {<br/> &nbsp; scanf(&quot;%s%d%d%s&quot;,&amp;stud[i].name,&amp;stud[i].num,&amp;stud[i].age,&amp;stud[i].addr);<br/> &nbsp; save();<br/> }<br/> for(i=0;i&lt;SIZE;i++)<br/> {<br/> &nbsp; printf(&quot;%s,%d,%d&quot;,stud[i].name,stud[i].num,stud[i].age,stud[i].addr);<br/> }<br/>}<br/><br/><br/> <br/><br/> <br/><br/>fread.c<br/><br/>#include &lt;stdio.h&gt;<br/>#define SIZE 2<br/>struct student_type<br/>{<br/> char name[10];<br/> int num;<br/> int age;<br/> char addr[10];<br/>}stud[SIZE];<br/>void read()<br/>{<br/> FILE *fp;<br/> int i;<br/> if((fp=fopen(&quot;stu_list&quot;,&quot;rb&quot;))==NULL)<br/> {<br/> &nbsp;printf(&quot;cant open the file&quot;);<br/> &nbsp;exit(0);<br/> }<br/> for(i=0;i&lt;SIZE;i++)<br/> {<br/> &nbsp; if(fread(&amp;stud[i],sizeof(struct student_type),1,fp)!=1)<br/> &nbsp; &nbsp;printf(&quot;file write error&#92;n&quot;);<br/> }<br/> fclose(fp);<br/>}<br/>main()<br/>{<br/><br/> int i;<br/> read();<br/> for(i=0;i&lt;SIZE;i++)<br/> {<br/> &nbsp; printf(&quot;%s,%d,%d,%s&quot;,stud[i].name,stud[i].num,stud[i].age,stud[i].addr);<br/> &nbsp; printf(&quot;&#92;n&quot;);<br/> }<br/>}<br/> <br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] C/C++文件的操作（fread() fwrite()）]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>