【发布时间】:2014-09-11 03:31:28
【问题描述】:
我正在尝试创建一个程序来使用环境变量中的值打开文件..
我的代码:
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<cstring>
using namespace std;
void main(int argc, char *argv[])
{
std::stringstream stream;
int a;
cout<<"Press 1 to open Remainder: "<<endl;
cout<<"Press any other key to exit: "<<endl;
cin>>a;
if(a==1)
{
system("\"C:\\Documents and Settings\\%USERNAME%\\My Documents\\CorelDRAW X3.txt\"");
// system(stream.str().c_str());
}
else
{
exit(0);
}
}
这是输出:
Press 1 to open Remainder:
Press any other key to exit:
1
'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
但是当我输入我的用户名时,它会完美运行..
我想在另一台电脑上使用这个程序,所以我使用了环境变量
有什么帮助吗?
谢谢。
【问题讨论】:
-
使用批处理文件会更容易。正如在一些答案中已经指出的那样,解决问题的一种方法是引用路径。另一种方法是使用短名称。在命令解释器中找到当前目录的短路径的一种方法是
for /d %d in (.) do @echo %~fsd。