C++ C++;以开头的路径~
这里有没有可能在linux中的c++代码中使用以“~”开头的路径?例如,此代码工作不正常:C++ C++;以开头的路径~,c++,path,tilde,C++,Path,Tilde,这里有没有可能在linux中的c++代码中使用以“~”开头的路径?例如,此代码工作不正常: #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open ("~/example.txt"); myfile << "Text in file .\n"; myfile.close(); return
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("~/example.txt");
myfile << "Text in file .\n";
myfile.close();
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
流文件;
myfile.open(“~/example.txt”);
myfile我猜您是在Linux或POSIX系统上,具有交互式shell理解~
(例如bash
)
实际上,以~
开头的文件路径几乎从未出现过(您可以在shell中使用mkdir'~'
创建这样一个目录,但这是不正确的)当你在你的终端中键入<代码> MyStudio~/Simult.txt < /Cord>作为你的终端中的命令时,用“代码> > /home /Martin < /Cord>取代.<代码> > />代码。请参见你的C++程序(但如果只需要<代码>”/Task.txt,你就需要这样做。
字符串来自一些数据-例如一些配置文件、一些用户输入等…)
有时,您可能只是使用获取主目录(或使用)
std::string home=getenv("HOME");
std::string path= home+"/example.txt";
ofstream myfile(path);
如果您是认真的,您应该检查getenv(“HOME”)
是否返回NULL
。实际上,这种情况不太可能发生
另请参见。工作目录中是否有名为
~
的目录?在不存在的目录中打开文件不会自动为您创建目录。例如mkdir\~
。