创建临时文件。 这是 tmpfile 版本,具有 CRT 中的安全性功能中所述的安全性增强功能。
语法
errno_t tmpfile_s(
FILE** pFilePtr
);
参数
pFilePtr
指针地址,用于存储生成的指向流的指针的地址。
返回值
如果成功,则返回 0;如果失败,则为错误代码。
错误条件
pFilePtr |
返回值 | pFilePtr 的内容 |
|---|---|---|
NULL |
EINVAL |
未更改 |
如果出现以上参数验证错误,则调用无效参数处理程序,如参数验证中所述。 如果允许继续执行,则将 errno 设置为 EINVAL,且返回值为 EINVAL。
备注
tmpfile_s 函数创建临时文件,并在 pFilePtr 参数中将指针放入该流。 在根目录中创建了临时文件。 若要在根目录以外的目录中创建临时文件,请结合使用 tmpnam_s 或 tempnam 与 fopen。
如果无法打开该文件,tmpfile_s 会将 NULL 写入 pFilePtr 参数。 当文件关闭、程序正常终止或调用 _rmtmp 时,此临时文件将被自动删除(假定当前工作目录未更改)。 临时文件在 w+b(二进制读/写)模式下打开。
如果尝试使用 tmpfile_s 执行超过 TMP_MAX_S 次调用(请参阅 STDIO.H),则可能失败。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
要求
| 例程 | 必需的标头 |
|---|---|
tmpfile_s |
<stdio.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
注意
此示例可能需要在 Windows 上运行的管理员特权。
// crt_tmpfile_s.c
// This program uses tmpfile_s to create a
// temporary file, then deletes this file with _rmtmp.
//
#include <stdio.h>
int main( void )
{
FILE *stream;
char tempstring[] = "String to be written";
int i;
errno_t err;
// Create temporary files.
for( i = 1; i <= 3; i++ )
{
err = tmpfile_s(&stream);
if( err )
perror( "Could not open new temporary file\n" );
else
printf( "Temporary file %d was created\n", i );
}
// Remove temporary files.
printf( "%d temporary files deleted\n", _rmtmp() );
}
Temporary file 1 was created
Temporary file 2 was created
Temporary file 3 was created
3 temporary files deleted