c - While writing into a file , Where intermediate data stored in between fopen() and fclose()? -


below small program takes information user , write file teacher.txt. using 1 array q2[30] taking input , writing file using fprintf(). when want enter more teacher again loop execute @ time fclose() not appear data not write/save(don't know) file previous value of q2 erased/overwrite new input. in case data stored/write fprintf().because when manually open teacher.txt before fclose() there no new data.

#include <conio.h> #include <iostream.h>   int main() {  system("cls");  int yoe;  char cond[]="yes";char q2[30];   file *p;  p = fopen("teacher.txt","a+"); //opening file in reading + appending mode  printf("\ndo want add more teacher ? (yes/no)\n");  gets(q2);  fflush(stdin);  if(!strcmp(q2,cond))  {           {          printf("\nenter teacher's name\n");          gets(q2);          fprintf(p,"\n!%s!",q2);          printf("enter teacher's qualifications\n");          fflush(stdin);          gets(q2);          fprintf(p,"%s!",q2);          printf("enter teacher's year of experience (0-30)\n");          fflush(stdin);          scanf("%d",&yoe);          fprintf(p,"%d!",yoe);          printf("enter teacher's mobile number(id) [should of 10 digits]\n");          fflush(stdin);          gets(q2);          fprintf(p,"%s!",q2);          printf("\ndo want add more teacher ? (yes/no)\n");          fflush(stdin);          gets(q2);      }while(!strcmp(q2,cond));  // condition check , if user want add more teacher , if yes loop execute again.      fclose(p);  // when user enter 'no' fclose appear.  }  fclose(p);printf("\npress key return admin menu\n");  getch();  system("pause"); } 

when open file fopen, output write buffered, meaning it's not send lower layers until buffer either full or explicitly flush fflush.

also note layers under fopen/fprintf/fclose, way down actual hardware, may also have buffering can delay actual update of on-disk data.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -