加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何使用tmpfile()获取临时文件名

发布时间:2020-12-16 06:51:19 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is there a way to get the filename from a `FILE*`? ????????????????????????????????????2个 tmpfile()函数说: The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates
参见英文答案 > Is there a way to get the filename from a `FILE*`? ????????????????????????????????????2个
tmpfile()函数说:

The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates normally. If the program terminates abnormally,whether the file is deleted depends on the specific system and library implementation.”

tmpfile()函数返回一个指向创建的临时文件的流指针,而不是文件的路径.
我需要临时文件名路径,因为它需要传递其他库函数.

我的应用程序可以异常退出,因此tmpfile()函数可以在异常退出时工作.

如何在退出时获取临时文件路径和文件自动删除

解决方法

您可以使用tmpname()代替使用tmpfile().

它将返回一个可用于创建临时文件的文件名.

请参阅以下示例(从http://www.cplusplus.com/reference/cstdio/tmpnam/中提取):

#include <stdio.h>

int main ()
{
   char buffer [L_tmpnam];
   char *pointer;

   tmpnam (buffer);
   printf ("Tempname #1: %sn",buffer);

   pointer = tmpnam (NULL);
   printf ("Tempname #2: %sn",pointer);

   return 0;  
}

然后,您可以使用此文件名创建文件并在退出时将其删除.

编辑:

> tmpnam返回的名称未指定任何路径. fopen使用的默认路径将是您当前的工作目录.
>您可以删除该文件.它不会自动完成.您可以拨打remove进行操作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读