示例:写到尾再从头读
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10], b[10], i;
FILE *iofile;
if((iofile=fopen("f1.dat","w+"))==NULL) /*用读写方式打开*/
{
printf("Cannot open file!");
exit(1);
}
printf("enter 10 integer numbers:\n");
for(i=0; i<10; i++)
{
scanf("%d", &a[i]);
fprintf(iofile, "%d ", a[i]);
}
printf("The numbers have been writen to file. \n");
printf("Display the data by read from file: \n");
fseek(iofile, 0, SEEK_SET); //亦可rewind(iofile);
for(i=0; i<10; i++)
{
fscanf(iofile, "%d", &b[i]);
printf("%d ", b[i]);
}
printf("\n");
fclose(iofile);
return 0;
}