0

I'm writing a code for a simple phone book. Everything works fine except that after successfully deleting an entry using my delete function my append function cant seem to write entries to the file anymore. unless I delete the database.data file that I'm using to store the entries.

NOTE: the 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 the line to be 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 successfully deleted!!");

}

heres the function for append:

void append(struct entry *ptr)
{

FILE *filepointer;

filepointer=fopen(file,"a+");

    fflush(stdin);              //This block is asking for the inputs to be placed into the 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 does 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 successfully written!\n");
}

struct entry {

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

if you want more details please do tell me.

UPDATE-

I narrowed down the problem to the while loop in the delete function my append function seems to work after using delete if the contents in the while loop were written like this:

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

But if the while loop were written in this way it would delete all the entries following the specified line. whereas in my previous code for the while loop in the deletee function it only deletes that specific line, but as stated the problem of the append function not being able to write to the file will persist until I delete the file "database.data" manually.

Slaine
  • 13
  • 1
  • 3

1 Answers1

0

Solved the problem turns out that the append function was able to write entries into the file the only problem is my print function couldnt print out the new entries due to the delete function leaving garbage after being executed. revised the code so that no garbage would be 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 the line to be 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 successfully deleted!!");

}

Slaine
  • 13
  • 1
  • 3