C十一:fopen, fread,fwrite
阿新 • • 發佈:2018-11-04
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #define DATA_SIZE 1024 int writeTest(){ unsigned int *dataPtr = NULL; dataPtr = (unsigned int *)malloc(sizeof(int)*DATA_SIZE); for(unsigned int i=0;i<DATA_SIZE;i++) { dataPtr[i] = i; //初始化快取區 } FILE *fp = fopen("test.txt","wb"); fwrite(dataPtr,sizeof(int),DATA_SIZE,fp); fclose(fp); free(dataPtr); system("pause"); return 0; } int readTest(){ FILE *fp = fopen("test.txt","rb"); int temp; int res; while(1){ res = fread(&temp,sizeof(int),1,fp); if (res == 1) printf("%d\n",temp); else break; if(temp>200) break; } fclose(fp); return 0; } int main() { writeTest(); readTest(); }