c - Why does my append function no longer write to file -


i'm writing code simple phone book. works fine except after deleting entry using delete function append function cant seem write entries file anymore. unless delete database.data file i'm using store entries.

note: character array file="database.data"

delete function:

void deletee() {     int tode,count;     char ch;     printc();     count=1;      file *filepointer,*filepointer2;      filepointer=fopen(file,"r+");     filepointer2=fopen("datatemp.data","w+");     if(filepointer==null)     {         printf("error error!");     }      printf("enter line number of line deleted: \n");     scanf("%d",&tode);     while (ch!=eof)     {         ch=getc(filepointer);         if(ch=='\n')         {             count++;         }         if(count!=tode)         {                 fprintf(filepointer2,"%c",ch);         }     } fclose(filepointer); fclose(filepointer2); remove(file); rename("datatemp.data",file); printf("content deleted!!");  } 

heres function append:

void append(struct entry *ptr) {  file *filepointer;  filepointer=fopen(file,"a+");      fflush(stdin);              //this block asking inputs placed file     printf("enter fname: ");     scanf("%s",&ptr->fn);     printf("\nenter lname: ");     scanf("%s",&ptr->ln);     printf("\nenter mname: ");     scanf("%s",&ptr->mn);     printf("\nenter bd: ");     scanf("%s",&ptr->bd);     printf("\nenter cnum: ");     scanf("%s",&ptr->cn);  if(filepointer==null) {     printf("the file not exist.\n");     return; }      system("cls");     fprintf(filepointer,"%15s%15s%15s%9s%11s\n",ptr->fn,ptr->ln,ptr->mn,ptr->bd,ptr->cn);     fclose(filepointer);     printf("entries written!\n"); } 

struct entry {

  char fn[15];     char ln[15];     char mn[15];     char bd[9];     char cn[11]; }p; 

if want more details please tell me.

update-

i narrowed down problem while loop in delete function append function seems work after using delete if contents in while loop written this:

    while (ch!=eof)     {         ch=getc(filepointer);         if(count!=tode)         {                 fprintf(filepointer2,"%c",ch);             if(ch=='\n')             {                 count++;             }         }     } 

but if while loop written in way delete entries following specified line. whereas in previous code while loop in deletee function deletes specific line, stated problem of append function not being able write file persist until delete file "database.data" manually.

solved problem turns out append function able write entries file problem print function couldnt print out new entries due delete function leaving garbage after being executed. revised code no garbage written after deleting.

void deletee()

{

    int tode,count;     char ch,sc;     printc();     count=1;      file *filepointer,*filepointer2;      filepointer=fopen(file,"r+");     filepointer2=fopen("datatemp.data","w+");     if(filepointer==null)     {         printf("error error!");     }     printf("enter line number of line deleted: \n");     scanf("%d",&tode);     while (ch!=eof)     {         ch=getc(filepointer);         if(count!=tode)         {                 if(ch==eof)                     {                     break;                     }                 fprintf(filepointer2,"%c",ch);          }             if(ch=='\n')             {                 count++;             }      } fclose(filepointer); fclose(filepointer2); swap(); remove("datatemp.data"); printf("content deleted!!"); 

}


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -