2

I have this function:

void aggiornadatabase(void) {
    FILE* fp;
    int c=0;
    char str[30];
    int m;

    sprintf(str, "%s.csv", utenti[posizioneuser].id);
    printf("%s\n", str);
    fp = fopen(str, "w");
    if (fp == NULL)
        printf("Database error\n");
    else
        m = remove(str);

    if (m == 0)
        printf("Success\n");
    else
        printf("Unable to delete the File\n");

    fclose(fp);
}

When this function executes, it deletes everything in the selected .csv file, but it does not delete the file itself (and indeed it prints "Unable to delete the File").

Why this is happening?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Pikappa
  • 284
  • 1
  • 11

4 Answers4

6

The file remains open, and deleting an open file is implementation-defined, so, deletion may or may not succeed. You'd better close the file before attempting to remove it.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • Well, I added a fclose(fp) before the remove but still not working well. Everything into the file is deleted except for the file himself – Pikappa May 31 '17 at 18:00
  • @Pikappa, how do you know that? Do you mean you still can `fopen` the file without any errors? – ForceBru May 31 '17 at 18:07
  • Do you have another remove() linked that deletes everything inside a file? Try unlink() instead, see if that works. – ThingyWotsit May 31 '17 at 18:11
  • @Pikappa, If you are on a system (e.g. Windows) that objects to deleting a file when a process has it open, then failure to delete is a routine possibility you just have to live with. Even if the process attempting the deletion does not have it open, deletion can fail because some other process does. – John Bollinger May 31 '17 at 18:30
  • 1
    @Pikappa, Emptying the file in the event of a failure to delete is a surprising behavior, but I hesitate to comment on it without more information about the details of how such behavior was observed. – John Bollinger May 31 '17 at 18:33
  • Already tried with unlink(), same thing as remove(), the function emptying the file without deleting it. Don't really know what's happening – Pikappa May 31 '17 at 18:45
  • @Pikappa please show the revised code, added to the question as an edit. – Weather Vane May 31 '17 at 18:47
  • 1
    @JohnBollinger `fopen("...", "w");` already truncates the file, not the attempt to delete it. – Ctx May 31 '17 at 19:32
2

Your code is messy and I think the last fclose(fp) is what's giving you this weird behaviour. First of all, you should always initialise variables, in your example m would generate an undefined behaviour when entering the second if statement (which should be inside the first else statement).

Also, make sure to open the file for reading not for writing.

//sprintf(...)
fp = open(str, "r");
if(fp == NULL) printf("Database error\n");
else {
    fp.close()
    if(remove(str)) printf("Unable to delete the file\n");
    else printf("Delete successful\n");
}

You shouldn't add the last fclose(fp) since when you arrive to that point, it's already closed or it wasn't even opened in first place.

lpares12
  • 3,504
  • 4
  • 24
  • 45
0

Instead of letting us guess why the file wasn't removed, use the perror() function yourself to get a readable and possibly understandable error message:

if (remove(str) == 0)
    printf("Success\n");
else
    perror("Unable to delete the file");

Notice also that your logic is faulty: if the DB file cannot be opened for writing, the message "Database error\n" will be printed; and m will remain unset, causing undefined behaviour!

0

Using the file's path/name string instead of the pointer to the file in remove() should work.