C 语言 【fread fwrite】

 1 #include <stdio.h>
 2 void main( void )
 3 {
 4     FILE *stream;
 5     char list[30];
 6     int i, numread, numwritten;
 7     // 以文本方式打开文件
 8     if( (stream = fopen( "test.txt", "w+t" )) != NULL )  // 如果读取无误
 9     {
10         for ( i = 0; i < 25; i++ )    //循环
11         list[i] = (char)('z' - i);    //生成字母
12         numwritten = fwrite( list, sizeof( char ), 25, stream );
13         printf( "Wrote %d items\n", numwritten );
14         fclose( stream );
15     }
16     else
17     {
18         printf( "Problem opening the file\n" );
19     }
20     if( (stream = fopen( "test.txt", "r+t" )) != NULL )  // 文件读取
21     {
22         numread = fread( list, sizeof( char ), 25, stream );  //读取的数据存储在list
23         printf( "Number of items read = %d\n", numread );
24         printf( "Contents of buffer = %.2ss\n", list );  //输出25个字符
25         fclose( stream );
26     }
27     else
28     {
29         printf( "File could not be opened\n" );
30     }
31 }

 

posted @ 2018-08-31 22:19  Justice-V  阅读(152)  评论(0)    收藏  举报