How to delete the file pointed to by FILE * in C?

#include<stdio.h>

int main() {

    FILE* fp;
    fp = fopen("temp.txt", "w");
    fprintf(fp, "Hello, World!\n");

    // remove("temp.txt");  this requires the filename as an argument
    // removefile(fp);      <--- is something like this possible?

    return 0;
}

The delete function ( defined in stdio.h ) accepts the file name as a parameter, but not the file pointer itself.

Is there any function in the standard C libraries that does file deletion and takes a file pointer as an argument?

+5
source share
5 answers

I do not believe that this can be done because FILE * may not correspond to a file in the file system at all (for example, stdin and stdout).

And on file systems that support hard links, can there be several paths that link to the same base file that you want to delete?

+7
source

'fILE * tmpfile (void)' stdlib.

:

tmpfile() . , . , fopen() update (w +).

, tmpfile() , tmpfile().

, .

+7

, ( ).

+1

, . FILE struct . , FILE char*

+1

, , , ?

+1

All Articles