⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_46152.htm

📁 vc
💻 HTM
字号:
<p>
序号:46152 发表者:anhongbo 发表日期:2003-07-07 15:06:12
<br>主题:请问:fwrite和 fread的用法!
<br>内容:各位大侠:fwrite和 fread怎么用?各自的参数是什么?多谢了!!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:风之文章 回复日期:2003-07-07 15:48:38
<br>内容:Reads data from a stream.<BR><BR>size_t fread( void *buffer, size_t size, size_t count, FILE *stream );<BR><BR>Function Required Header Compatibility <BR>fread &lt;stdio.h&gt; ANSI, Win 95, Win NT <BR><BR><BR>For additional compatibility information, see Compatibility in the Introduction.<BR><BR>Libraries<BR><BR>LIBC.LIB Single thread static library, retail version <BR>LIBCMT.LIB Multithread static library, retail version <BR>MSVCRT.LIB Import library for MSVCRT.DLL, retail version <BR><BR><BR>Return Value<BR><BR>fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.<BR><BR>Parameters<BR><BR>buffer<BR><BR>Storage location for data<BR><BR>size<BR><BR>Item size in bytes<BR><BR>count<BR><BR>Maximum number of items to be read<BR><BR>stream<BR><BR>Pointer to FILE structure<BR><BR>Remarks<BR><BR>The fread function reads up to count items of size bytes from the input stream and stores them in buffer. The file pointer associated with stream (if there is one) is increased by the number of bytes actually read. If the given stream is opened in text mode, carriage return–linefeed pairs are replaced with single linefeed characters. The replacement has no effect on the file pointer or the return value. The file-pointer position is indeterminate if an error occurs. The value of a partially read item cannot be determined.<BR><BR>Example<BR><BR>/* FREAD.C: This program opens a file named FREAD.OUT and<BR> * writes 25 characters to the file. It then tries to open<BR> * FREAD.OUT and read in 25 characters. If the attempt succeeds,<BR> * the program displays the number of actual items read.<BR> */<BR><BR>#include &lt;stdio.h&gt;<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; FILE *stream;<BR>&nbsp;&nbsp; char list[30];<BR>&nbsp;&nbsp; int&nbsp;&nbsp;i, numread, numwritten;<BR><BR>&nbsp;&nbsp; /* Open file in text mode: */<BR>&nbsp;&nbsp; if( (stream = fopen( "fread.out", "w+t" )) != NULL )<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( i = 0; i &lt; 25; i++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list[i] = (char)('z' - i);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Write 25 characters to stream */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numwritten = fwrite( list, sizeof( char ), 25, stream );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Wrote %d items\n", numwritten );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose( stream );<BR><BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Problem opening the file\n" );<BR><BR>&nbsp;&nbsp; if( (stream = fopen( "fread.out", "r+t" )) != NULL )<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Attempt to read in 25 characters */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numread = fread( list, sizeof( char ), 25, stream );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Number of items read = %d\n", numread );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Contents of buffer = %.25s\n", list );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose( stream );<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "File could not be opened\n" );<BR>}<BR><BR><BR>Output<BR><BR>Wrote 25 items<BR>Number of items read = 25<BR>Contents of buffer = zyxwvutsrqponmlkjihgfedcb<BR><BR><BR>fwrite<BR>Writes data to a stream.<BR><BR>size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );<BR><BR>Function Required Header Compatibility <BR>fwrite &lt;stdio.h&gt; ANSI, Win 95, Win NT <BR><BR><BR>For additional compatibility information, see Compatibility in the Introduction.<BR><BR>Libraries<BR><BR>LIBC.LIB Single thread static library, retail version <BR>LIBCMT.LIB Multithread static library, retail version <BR>MSVCRT.LIB Import library for MSVCRT.DLL, retail version <BR><BR><BR>Return Value<BR><BR>fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.<BR><BR>Parameters<BR><BR>buffer<BR><BR>Pointer to data to be written<BR><BR>size<BR><BR>Item size in bytes<BR><BR>count<BR><BR>Maximum number of items to be written<BR><BR>stream<BR><BR>Pointer to FILE structure<BR><BR>Remarks<BR><BR>The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair. The replacement has no effect on the return value.<BR><BR>Example<BR><BR>/* FREAD.C: This program opens a file named FREAD.OUT and<BR> * writes 25 characters to the file. It then tries to open<BR> * FREAD.OUT and read in 25 characters. If the attempt succeeds,<BR> * the program displays the number of actual items read.<BR> */<BR><BR>#include &lt;stdio.h&gt;<BR><BR>void main( void )<BR>{<BR>&nbsp;&nbsp; FILE *stream;<BR>&nbsp;&nbsp; char list[30];<BR>&nbsp;&nbsp; int&nbsp;&nbsp;i, numread, numwritten;<BR><BR>&nbsp;&nbsp; /* Open file in text mode: */<BR>&nbsp;&nbsp; if( (stream = fopen( "fread.out", "w+t" )) != NULL )<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( i = 0; i &lt; 25; i++ )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list[i] = (char)('z' - i);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Write 25 characters to stream */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numwritten = fwrite( list, sizeof( char ), 25, stream );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Wrote %d items\n", numwritten );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose( stream );<BR><BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Problem opening the file\n" );<BR><BR>&nbsp;&nbsp; if( (stream = fopen( "fread.out", "r+t" )) != NULL )<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Attempt to read in 25 characters */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numread = fread( list, sizeof( char ), 25, stream );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Number of items read = %d\n", numread );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "Contents of buffer = %.25s\n", list );<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose( stream );<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf( "File could not be opened\n" );<BR>}<BR><BR><BR>Output<BR><BR>Wrote 25 items<BR>Number of items read = 25<BR>Contents of buffer = zyxwvutsrqponmlkjihgfedcb<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:anhongbo 回复日期:2003-07-08 07:22:18
<br>内容:这不是帮助上的例子吗?可我看不懂E文呀:)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:bird 回复日期:2003-07-08 08:51:26
<br>内容: fread函数和fwrite函数可以用来读写一个数据块。它们一般调用形式为:<BR><BR>fread (buffer,size,count,fp);<BR><BR>fwrite(buffer,size,count,fp);<BR><BR>buffer:是一个指针。对fread它是读入数据的存放地址。对fwrite是输出数据的地址。<BR><BR>size:要读写的字节数。<BR><BR>count:要进行读写多少个size字节的数据项。<BR><BR>fp:文件型指针。<BR><BR>如果文件以二进制形式打开,用fread和fwrite函数就可以读写任何类型的信息,如 fread(f,4,2,fp); 其中f是一个实型数组名。一个实型变量占4个字节。这个函数从fp所指向的文件读入2次数据,存储到数组f中。如果有如下一个结构体类型:<BR><BR>sturt student_type<BR><BR>{ char name[10];<BR><BR>int num;<BR><BR>int age;<BR><BR>char addr[30];<BR><BR>} stud[40]; 结构体数组stud共40个元素,每一个元素用来存放一个学生数据,假设学生的数据已存放在磁盘文件中,可以用下面的语句读入40个学生数据。<BR><BR>for(i = 0; i&lt;40;i++)<BR><BR>fread(&amp;stud[i],sizeof(struct student_type),1,fp);<BR><BR>同样,以下的语句可以将内存中学生数据输出到磁盘文件中。<BR><BR>for (i=0;i&lt;40;i++)<BR><BR>fread(&amp;stud[i],sizeof(struct student_type),1,fp);<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:bird 回复日期:2003-07-08 08:52:12
<br>内容:建议好好补习E文!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:bird 回复日期:2003-07-08 08:52:50
<br>内容:写出一个完整的程序。<BR><BR>例:从键盘输入4个学生的有关数据,然后把它们转存到磁盘文件中去。<BR><BR># include &lt;stdio.h&gt;<BR><BR>#define SIZE 4<BR><BR>struct student_type<BR><BR>{ char name[10];<BR><BR>int num;<BR><BR>int age;<BR><BR>char addr[15];<BR><BR>} stud[SIZE];<BR><BR>void save( void)<BR><BR>{ FILE *fp;<BR><BR>int i;<BR><BR>if ((fp=fopen("stu_list","wb"))= =NULL)<BR><BR>{<BR><BR>print("cannot open file\n");<BR><BR>return;<BR><BR>}<BR><BR>for(i=0;i&lt;SIZE;i++)<BR><BR>if(fwrite(&amp;stud[i],sizeof(struct student_type),1,fp)!=1)<BR><BR>printf("file write error\n");<BR><BR>}<BR><BR>void main(void)<BR><BR>{ int i;<BR><BR>for (i=0;i&lt;SIZE;i++)<BR><BR>scanf("%s%d%d%s",stud[i].name,&amp;stud[i].num,&amp;stud[i].age, stud[i].addr);<BR><BR>save( );<BR><BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -