#include<stdio.h>
main()
{FILE*fp;
int i=10,j=20;
int a,b;
if((fp=fopen("s.txt","w"))==NULL)
{printf("can't open file\n");
exit(0);
}
fprintf(fp,"%d\n%d",i,j);
fclose(fp);
if((fp=fopen("s.txt","r"))==NULL)
{printf("can't open file\n");
exit(0);
}
scanf(fp,"%d\n",&a,&b);
fclose(fp);
printf("%d %d",a,b);
}
而输出的a,b并不是10,20;
请说明原因,谢谢!
參考答案:scanf(fp,"%d\n",&a,&b);
->
fscanf(fp,"%d\n%d",&a,&b);
(因为你用 fprintf 写入,而且,你还少了个%d)