C 语言 fwrite 和 fread 文件读写操作示例


C 语言 fwrite 和 fread 文件读写操作示例
  1. #include <stdio.h>   
  2. int main()  
  3. {  
  4.     FILE* pFile;  
  5.     float buffer[] = { 2.0 , 3.0 , 8.0 };  
  6.     pFile = fopen("myfile.bin" , "wb"); // 打开文件写操作   
  7.     fwrite(buffer , 1 , sizeof(buffer) , pFile); // 把浮点数组写到文件 myfile.bin   
  8.     fclose(pFile); // 关闭文件   
  9.   
  10.     float read[3];  
  11.     pFile = fopen("myfile.bin" , "rb"); // 重新打开文件读操作   
  12.     fread(read , 1 , sizeof(read) , pFile); // 从文件中读数据   
  13.     printf("%f\t%f\t%f\n", read[0], read[1], read[2]);  
  14.   
  15.     fclose(pFile); // 关闭文件   
  16.     return 0;  
  17. }  
  • 1
  • 2
  • 下一页

相关内容