#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 remove function (defined in stdio.h) takes the file name as a parameter, but not the file pointer itself.
Is there some function in the C standard libraries that does file deletion, and takes file pointer as the arguement?
No, you can't. And
FILE
struct doesn't include filename inside it. So best option is to have structure that will both hold pointer toFILE
and tochar*
with nameI don't believe there's any way to do this, because a FILE* may not necessarily correspond to a file in the filesystem at all (For example, stdin and stdout).
And in filesystems that support hard links, there can be multiple paths referring to the same underlying file, which one would you want it to remove?
No, there isn't (unfortunately).
You may want to use the 'FILE * tmpfile(void)' function from stdlib.
from the man:
You closed pointer, then her value was freed, how you imagine delete file by this handle?