1

在我的程序中,需要的部分资源是存储数据的目录。按照惯例,我决定制作这个目录~/.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;
}

~现在,至少使用in的可靠性值得mkdir("~/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)怀疑,所以我真正想做的是提示输入用户名,将其存储在string(like string usern; cin >> usern;) 中,然后执行mkdir("/home/{$USERN}/.program/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)(like in shell)。但是,我不知道如何将 $USERN 的等价物放入字符串中,例如,我不知道如何将可扩展的 c++ 构造放入字符串中。我的意思是我将任何“形式”的变量插入到字符串中,该变量的内容会扩展为该变量的内容。

如果这个问题令人困惑,我深表歉意,我似乎无法很好地解释我到底想要什么。

或者,更可取的是,是否可以在不提示的情况下获取用户名?(当然,并将其存储在一个字符串中)

4

2 回答 2

4

你可以使用:

std::string usern;
std::cin >> usern;
std::string directory = "/home/" + usern + "/.program";
mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

一个更好的选择,IMO,是使用环境变量 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");
}
于 2017-07-17T19:03:16.717 回答
1

拥有用户名 in usern,您可以以任何您想要的方式构造字符串:

std::string dir = "/home/" + usern + "/.program/"

mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

要在不提示用户的情况下获取用户名,您可以使用getlogin_r()

于 2017-07-17T19:10:33.920 回答