#include <stdio.h> #include <stdlib.h> ////////////////////////////////////////////////////////////////////// //+------------------------------------------------------------------- // Kitty PaoPao // Copyright (C) KittyPaoPao Corporation, 2005-2008. // // 版权所有:KittyPaoPao 时空之蕊 // // Contents: 欢迎拷贝 (请保留版权信息) 谢谢! //-------------------------------------------------------------------- void main() { const char * fileName = "x:\\test.dat"; FILE * file = NULL; double temp = 0.0, sum = 0.0, maxD = 10000; int i = 0, number = 100;
file=fopen( fileName, "wb" ); if ( NULL == file ) { printf( "创建文件失败!\n" ); return; } for ( i = 0; i < number; i++ ) { temp = rand() * maxD / RAND_MAX; if ( fwrite( &temp, sizeof( temp ), 1, file ) != 1 ) { printf( "数据写入错误!\n" ); fclose( file ); return; } } fclose( file ); file=fopen( fileName, "rb" ); if ( NULL == file ) { printf( "读取文件失败!\n" ); return; } sum = temp = 0.0; for ( i = 0; i < number && !feof( file ) ; i++ ) { if ( fread( &temp, sizeof( temp ), 1, file ) != 1 ) { printf( "数据读取错误!\n" ); fclose( file ); return; } sum += temp; } fclose( file ); printf( "\nsum = %.5f\n", sum ); } // 谢谢使用 ///////////////////////////////////////////////////////////////////// 不好意思,请原谅!只要注意到模式就可以了
[此贴子已经被作者于2005-5-5 18:23:45编辑过]