我对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/