第一次运行程序时,会创建文件。但似乎while循环需要很长时间才能克服。因为文件现在是空的,所以它不会在文件的开头得到EOF吗?
#include<stdio.h>
void main(){
FILE *p;
int b, a=0;b=0;
p=fopen("text.txt", "a+");
while((b=fscanf(p,"%d",&a)) != EOF)
printf("%d\n",a);
fseek(p, 0, SEEK_END);
fprintf(p, " %d %d",1,6);
fflush(p);
fclose(p);
}
答案 0 :(得分:1)
#include<stdio.h>
int main(){
FILE *p;
int b=0, a=0;
p=fopen("text.txt", "a+");
while((b=fscanf(p,"%d",&a)) == 1)
printf("%d\n",a);
// no need to seek, or flush
fprintf(p, " %d %d",1,6);
fclose(p);
return 0;
}