#include "stdio.h"
main()
{ FILE *fp;int i,k,n;
fp=fopen("data1.dat","w+");
for (i=1;i<=6;i++)
{fprintf(fp,"%d ",i); ///这里%d后有空格,输出到文件中的数据是1 2 3 (第一行),fscanf读取时是以空格为分界的,所以只能读进来1,2
if(i%3==0) fprintf(fp,"\n");
}
rewind(fp);
fscanf(fp,"%d%d",&k,&n);printf("%d,%d\n",k,n);
fclose(fp);
}
main()
{ FILE *fp;int k,n,a[6]={1,2,3,4,5,6};
fp=fopen("d2.dat","w");
fprintf(fp,"%d%d%d\n",a[0],a[1],a[2]); ///这个输出到文件的格式为123 456
fprintf(fp,"%d%d%d\n",a[3],a[4],a[5]);
fclose(fp);
fp=fopen("d2.dat","r");;
fscanf(fp,"%d%d",&k,&n);
printf("%d,%d\n",k,n);
fclose(fp);
}
不认真,马虎~~