gpt4 book ai didi

c - 将 fopen 与 temp 系统变量一起使用

转载 作者:行者123 更新时间:2023-11-30 16:11:07 25 4
gpt4 key购买 nike

我对fopen有一些疑问...

我可以执行以下操作吗?

fopen("%temp%" , "r");

或者我需要使用 Windows 特定功能吗?

最佳答案

不,您不能直接执行此操作(除非您想打开名为 %temp 的文件)。有一个函数ExpandEnvironmentStrings这样做是这样的:

char path[MAX_PATH];
ExpandEnvironmentStrings("%TEMP%\\tempfile", path, MAX_PATH);
fopen(path, "r");

您可以手动执行此操作 - 在这种情况下它可以更便携:

char path[MAX_PATH];
const char* temp = getenv("TEMP");

if(temp == NULL)
; // Return an error or try to guess user's Temp
// directory with GetUserProfileDirectory or similiar functions

snprintf(path, MAX_PATH - 1, "%s\\tempfile", temp);

fopen(path , "r");

但是您的情况有一个更清洁选项 - tmpfile

关于c - 将 fopen 与 temp 系统变量一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58705490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com