Reputation: 157
When I run the program for the first time, the file gets created. But it seems the while loop takes a long time to get over. Doesnt it get an EOF in the beginning of the file since the file is empty as of now?
#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);
}
Upvotes: 0
Views: 108
Reputation: 97958
Make sure fscanf returns 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;
}
Upvotes: 1