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
  • Technically, doing `fflush(stdin)` is undefined behavior. Some systems have it as an extension, but it's not portable. – Some programmer dude Nov 19 '14 at 14:42
  • 2
    You also have [undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior) in the `deletee` function, as you use local variable `ch` before it's initialized. The value of uninitialized local non-static variables is indeterminate. – Some programmer dude Nov 19 '14 at 14:45
  • 1
    @JoachimPileborg Removed fflush(stdin) thanks for the tip. – Slaine Nov 19 '14 at 14:45
  • And when you delete a line using your function, you will end up with an empty line where it was. You might want to change order of those two blocks in the loop. – Some programmer dude Nov 19 '14 at 14:47
  • And finally, to help you figure out what's the problem, when `fopen` fails, you should print the value of `errno` to see what's wrong, or use e.g. `perror` or `strerrno` to print or get a printable string from the error code. – Some programmer dude Nov 19 '14 at 14:49
  • @JoachimPileborg I don't think fopen fails since it doesnt print out the "file doesnt exist" text i placed in the if statement. Is it still possible for fopen to fail? – Slaine Nov 19 '14 at 14:53
  • No, then the `fopen` functions seems to work. How do you check that the file was not updated? – Some programmer dude Nov 19 '14 at 15:06
  • Note: 1) `ch` is not initialized before first use. 2) Should use type `int ch`. 3) `fflush(stdin);` undefined behavior – chux - Reinstate Monica Nov 19 '14 at 18:47
  • Suggest posting `struct entry` definition. – chux - Reinstate Monica Nov 19 '14 at 18:51
  • Doubt code needs `"%15s%15s%15s%9s%11s\n"`. Maybe `"%15s %15s %15s %9s %11s\n"`. – chux - Reinstate Monica Nov 19 '14 at 18:54
  • @chux I'll edit the post now to include the struct definitions. – Slaine Nov 19 '14 at 22:47
  • Replace `scanf("%s",&ptr->fn);` with `scanf("%14s",...);` (in 4 places) and `scanf("%8s",&ptr->bd);` to stop input overflow. If this does not fix tings, suggest posting some example lines of data. But in general, assuming you fix things all ready commented on, you may need to post more of the higher level code. – chux - Reinstate Monica Nov 19 '14 at 23:03
  • @chux I updated the question I seemed to have been able to narrow it down to the while loop details are in the question – Slaine Nov 19 '14 at 23:36
  • 1) Be sure to use `int ch`. 2) Re-write loop `while ((ch=getc(filepointer)) != EOF) {` If this works I'll detail in answer if you like. – chux - Reinstate Monica Nov 20 '14 at 00:31
  • Try adding fflush(filepointer) before you close the file (good practice whenever you close a file) – alonewenson Nov 19 '14 at 16:01
  • Calling `fclose` will flush any unwritten data, so it isn't necessary to explicitly call `fflush` just before `fclose`. – Klas Lindbäck Nov 19 '14 at 16:03

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