将fopen与temp系统变量一起使用 [英] Using fopen with temp system variable

查看:102
本文介绍了将fopen与temp系统变量一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对fopen有一些疑问...

I had some doubts about fopen...

我可以执行以下操作吗?

Can i perform the following?

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

还是我需要使用Windows特定功能?

or do i need to use windows specific functions?

推荐答案

否,您不能直接执行操作(除非您要打开名为%temp的文件).有一个功能 ExpandEnvironmentStrings 可以做到这一点:

No, you cannot do directly (unless you want to open file called %temp). There is a function ExpandEnvironmentStrings that does that:

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

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

You can do that manually -- in this case it can be more portable:

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");

但是针对您的情况有一个 cleaner 选项-

But there is a cleaner option for your case -- tmpfile

这篇关于将fopen与temp系统变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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