Function "remove()" in C does not delete the selected file

Viewed 1623

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?

4 Answers

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

Related