当前位置:首页 > C语言课件
mode将从下列选项中选取:_IOFBF 表示完全缓冲(缓冲区满时刷新);_IOLBF 表示行缓冲(缓冲区满或一个新行写入时);_IONBF表示无缓冲。如果执行成功,函数会返回零值,否则返回非零值。
13.7.4 二进制I/O:fread( )和fwrite( )
如果要把数字数据保存到一个文件中该怎么做呢?确实,可以使用fprintf( )和%f 格式保存一个浮点值,不过那样就将它作为字符串存储了。例如,下面的序列
double num = 1./3.; fprintf( fp, \
将num作为一个8字符的字符串0.333333存储;使用%.2f说明符可以把它存储为4字符的字符串0.33;使用%.12f说明符可以把它存储为14字符的字符串0.333333333333。
总之,fprintf( ) 以一种可能改变数字值的方式将其转换成为字符串。
最精确和一致的存储数字的方法就是使用和程序所使用的相同的位格式。所以,一个double值就应该存储在一个double大小的单元中。如果把数据存储在一个使用与程序具有相同表示方法的文件中,就称数据以二进制形式存储。对于标准I/O,fread( )和fwrite( ) 函数提供了这种二进制服务。 13.7.5 size_t fwrite( )函数
fwrite( )的原型是:
size_t fwrite( const void * restrict ptr, size_t size, size_t nmemb, FILE * restrict fp ); fwrite( )函数将二进制数据写入文件。指针ptr是要写入的数据块的地址;size表示要写入的数据块的大小(以字节为单位);nmemb表示数据块的数目;像一般函数那样,fp指定要写入的文件。
要保存一个包含10个double值的数组,可以这样做: double earnings[ 10 ]; fwrite( earnings, sizeof( double ), 10, fp );
fwrite( )函数返回成功写入的项目数。正常情况下,它与nmemb,相等,但若有写入错误就会变小。
13.7.6 size_t fread( )函数
fread( )的原型是:
size_t fread( void * restrict ptr, size_t size, size_t nmemb, FILE * restrict fp );
fread( ) 函数的参数与fwrite( )相同。此时ptr为读入文件数据的内存存储地址;fp指定要读取的文件。使用这一函数来读取通过fwrite( )写入的文件数据。
要恢复前一例子中保存的包含10个double值的数组,可以使用以下函数调用: double earnings[10]; fread( earnings, sizeof( double ), 10, fp );
该调用将10个double值复制到earnings数组中。
fread( )函数返回成功读入的项目数,正常情况下,它与nmemb,相等,但若有读取错误或遇到文件尾就会变小。
13.7.7 int feof( FILE *fp )和 int ferror( FILE *fp )函数
当标准输入函数返回EOF时,通常表示已经到达了文件结尾,可是这也有可能表示发生了读取错误,使用feof( )和ferror( )函数可以区分这两种可能性。如果最近一次输入调用检测到文件结尾,feof( )函数返回一个非零值,否则返回零值。如果发生读写错误,ferror( )函数返回一个非零值,否则返回零值。
典型算法例子
一、正确循环读入字符
while ( ( c = getchar( ) ) != ' ' && c != '\\n' )
这个结构建立一个循环读入字符,直到出现第一个空格或换行符。第一个子表达式给c赋值,然后c的值被用在第二个字表达式中。如果没有顺序保障,计算机可能试图在c被赋值之前判断第二个字表达式。 二、循环输入
status = scanf( \ while ( status == 1 ) { sum += num;
printf( \ status = scanf( \ }
三、进制转换 #include
{ const char baseDigits[16]={ '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F' }; int convertedNumber[64], base, index=0; long int numberToConvert;
printf( \ scanf( \
printf( \ scanf( \
do { convertedNumber[index++]=numberToConvertose;
numberToConvert/=base; } while( numberToConvert ); printf( \ for ( --index; index>=0; --index )
printf( \ printf( \}
四、在已排序的数组中插入一个数字而不打乱其顺序。 int i, n, x, a[100]; puts( \scanf( \for( i=0; i { printf( \ scanf( \puts( \scanf( \for( i=n-1; i>=0; i-- ) if( x 五、将键盘输入数据转换为进制输出 #include void to_binary( unsigned long n ) /* 递归函数 */ { int r = n % 2; if ( n>=2 ) to_binary( n / 2 ); putchar( '0' + r ); } void main( void ) { unsigned long number; printf( \ while ( scanf( \ { printf( \ to_binary( number ); putchar( '\\n' ); printf( \ printf( \} 六、多次掷骰子的模拟程序 #include #include #include \用于roll_n_dice( )和roll_count */ void main( ) { int dice, roll, sides; srand( ( unsigned int )time( 0 ) ); /* 随机化种子 */ printf( \ while ( scanf( \ { printf( \ scanf( \ roll = roll_n_dice( dice, sides ); printf( \ \ printf( \ printf( \ roll_count ); /* 使用外部变量 */ printf( \} 七、一个fread( )和fwrite( )的例子 /* append.c ——追加多个文件内容到一个文件中 */ #include void append( FILE *source, FILE *dest ); int main( void ) { FILE *fa, *fs; /* fa指向追加的目的文件,fs指向源文件 */ int files = 0; /* 追加文件的个数 */ char file_app[ SLEN ]; /* 目的文件的名称 */ char file_src[ SLEN ]; /* 源文件的名称 */ puts( \ gets( file_app ); if ( ( fa = fopen( file_app, \ { fprintf( stderr, \ exit( 2 ); } if ( setvbuf( fa, NULL, _IOFBF, BUFSIZE ) != 0 ) { fputs( \ exit( 3 ); } puts( \ while ( gets( file_src ) && file_src[ 0 ] != '\\0' ) { if ( strcmp( file_src, file_app ) == 0 ) fputs( \ else if ( ( fs = fopen( file_src, \ fprintf( stderr, \ else { if ( setvbuf( fs, NULL, _IOFBF, BUFSIZE ) != 0) { fputs( \ continue; } append( fs, fa ); if ( ferror( fs ) != 0 ) fprintf(stderr, \ if ( ferror( fa ) != 0 ) fprintf(stderr,\ fclose( fs ); files++; printf( \ puts( \ } } printf( \ fclose( fa ); return 0; } void append( FILE *source, FILE *dest ) { size_t bytes; static char temp[BUFSIZE]; /* 分配一次 */ while( ( bytes = fread( temp, sizeof( char ), BUFSIZE,source ) ) > 0 ) fwrite( temp, sizeof ( char ), bytes, dest ); }
共分享92篇相关文档