如何删除一个文件在C中FILE *指向? [英] How can I delete a file pointed to by a FILE* in C?

查看:159
本文介绍了如何删除一个文件在C中FILE *指向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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;
}

删除功能(在stdio.h 定义)主罚文件名作为参数,而不是文件指针本身

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

有没有在做文件删除C标准库中的一些功能,并采取文件指针作为实际参数的?

Is there some function in the C standard libraries that does file deletion, and takes file pointer as the arguement?

推荐答案

您可能需要使用FILE * TMPFILE(无效)'从STDLIB功能。

You may want to use the 'FILE * tmpfile(void)' function from stdlib.

从男人:

说明

该TMPFILE()函数将创建一个
  临时文件并打开
  相应的流。该文件应
  自动当所有被删除
  对文件的引用被关闭。该
  文件在打开的fopen以()为
  更新(W +)。

The tmpfile() function shall create a temporary file and open a corresponding stream. The file shall be automatically deleted when all references to the file are closed. The file is opened as in fopen() for update (w+).

在一些实现中,一个永久
  文件可能如果工艺落后
  调用TMPFILE(),而它被杀害
  正在处理调用TMPFILE()。

In some implementations, a permanent file may be left behind if the process calling tmpfile() is killed while it is processing a call to tmpfile().

这是错误信息可被写入到
  标准错误,如果流不能
  打开。

An error message may be written to standard error if the stream cannot be opened.

这篇关于如何删除一个文件在C中FILE *指向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆