#include<stdio.h> #include<stdlib.h> void main() { FILE *fpIn,*fpOut; fpOut = fopen("shuzu.txt","wb"); //以二进制只写方式 if(NULL == fpOut) { printf("Error:Open file fail !\n"); exit(1); } int a[10] = {1,22,333,4444,55555,666666,7777777,88888888,999999999,1000000000}; fwrite(a,sizeof(int),10,fpOut); fpIn = fopen("shuzu.txt","rb"); //以二进制只读方式 if(NULL == fpIn) { printf("Error:Open file fail !\n"); exit(1); } while(1) { int i; int j; printf("请输入第i个数:\n"); scanf("%d",&i); fseek(fpIn,(i-1)*sizeof(int),SEEK_SET); fread(&j,sizeof(int),1,fpIn); printf("第 %d 个数为:%d\n",i,j); } fclose(fpOut); fclose(fpIn); }
时间: 2024-11-07 15:40:24