#include <stdio.h> #include <stdlib.h> int main(void) { struct testF{ int a; int b; float c; }; struct testF struct1[5]={{5, 4, 5.4}, {4, 3, 4.3}, {3, 2, 3.2}, {2, 1, 2.1}, {1, 0, 1.01}}; FILE *fpW; if((fpW=fopen("./testFile", "wb")) == NULL){ printf("Open file faied, yoou will exit this program.\n"); exit(1); } int i; for(i = 0; i< 5; i++){ fwrite(&struct1[i], sizeof(struct1[i], 1, fpW); } fclose(fpW); FILE *fpR; struct testF struct2; fseek(fpR, 0, SEEK_END); //将指针定在文件结尾处 long fSize = ftell(fpR); //返回文件的大小 rewind(fpR); //将指针重新定位在文件开始处 while(1){ fread(&struct2, sizeof(struct2), 1, fpR); printf("%d %d %f", struct2.a, struct2.b, struct2.c); if(ftell(fpR) == fSize) //判断是否已读到文件结尾处 break; } fclose(fpR); return 0; }
关于C语言中使用fread()读取整个文件的心得,布布扣,bubuko.com
时间: 2024-10-14 06:39:22