在我的程序中,需要的部分资源是存放数据的目录。按照惯例,我决定将此目录设为 ~/.program/
。在 C++ 中,创建此目录(在基于 UNIX 的系统上)的正确方法是以下代码:
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
using namespace std;
void mkworkdir()
{
if(stat("~/.program",&st) == 0)
{
cout << "Creating working directory..." << endl;
mkdir("~/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
mkdir("~/.program/moredata", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
else
{
cout << "Working directory found... continuing" << endl;
}
}
int main()
{
mkworkdir();
return 0;
}
现在,在 mkdir("~/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
中使用 ~
的可靠性至少是值得怀疑的,所以我真正想做的是提示输入用户名,将其存储在 string
中(如 string usern; cin >> usern;
),然后执行 mkdir("/home/{$USERN}/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
(就像在 shell 中一样)。但是,我不知道如何将 $USERN 的等价物放入字符串中,就像我不知道如何将可扩展的 c++ 构造放入字符串中一样。我的意思是,我将变量的任何“形式”插入到字符串中,该变量将扩展为该变量的内容。
如果这个问题令人困惑,我深表歉意,我似乎无法很好地解释我想要的到底是什么。
或者,更可取的是,是否可以在不提示用户名的情况下获取用户名? (当然,并将其存储在一个字符串中)
最佳答案
你可以使用:
std::string usern;
std::cin >> usern;
std::string directory = "/home/" + usern + "/.program";
mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
在我看来,更好的选择是使用环境变量 HOME 的值。
char const* home = std::getenv("HOME");
if ( home == nullptr )
{
// Deal with problem.
}
else
{
std::string directory = home + std::string("/.program");
mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
FWIW,您可以通过在应用程序的命名空间中创建函数 make_directory
来简化代码,您可以在其中添加检查目录是否存在、使用正确标志等的详细信息。
namespace MyApp
{
bool directory_exists(std::string const& directory)
{
struct stat st;
// This is simplistic check. It will be a problem
// if the entry exists but is not a directory. Further
// refinement is needed to deal with that case.
return ( stat(directory.c_tr(), &st) == 0 );
}
int make_directory(std::string const& directory)
{
if ( directory_exists(directory) )
{
return 0;
}
return mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
}
然后,您可以在其余代码中只使用 MyApp::make_directory
。
char const* home = std::getenv("HOME");
if ( home == nullptr )
{
// Deal with problem.
}
else
{
std::string directory = home + std::string("/.program");
MyApp::make_directory(directory);
MyApp::make_directory(directory + "/moredata");
}
关于c++ - 在字符串中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45151591/