C++:我需要目录导航方面的帮助

发布于 2024-11-05 05:42:31 字数 600 浏览 1 评论 0 原文

所以,我希望能够 chdir 进入目录(如果存在),如果不创建目录。如果我已经在目录中,则不需要执行任何操作。

例如

if (cur_dir == "dir_name")
// do stuff
else if ("dir_name" not exist?)
   mkdir "dir_name"
   chdir "dir_name"
else
   chdir "dir_name"

,我一直在谷歌搜索,到目前为止我已经想出了这个:

if (chdir(Config::CONFIG_FOLDER_NAME) == 0)
{
    std::cout << "Network Config Directory not found...\n";
    std::cout << "Creating folder called " << Config::CONFIG_FOLDER_NAME << "\n";
    mkdir(Config::CONFIG_FOLDER_NAME, 0777);
}

我还没有找到一种方法来检查当前目录是什么..(不是完整路径,这是我找到的。)

So, I want to be ableto chdir into a directory, if it exists, if not make the directory. If I am already in the directory, I just don't need to do anything.

Example

if (cur_dir == "dir_name")
// do stuff
else if ("dir_name" not exist?)
   mkdir "dir_name"
   chdir "dir_name"
else
   chdir "dir_name"

I've been googling, I've come up with this so far:

if (chdir(Config::CONFIG_FOLDER_NAME) == 0)
{
    std::cout << "Network Config Directory not found...\n";
    std::cout << "Creating folder called " << Config::CONFIG_FOLDER_NAME << "\n";
    mkdir(Config::CONFIG_FOLDER_NAME, 0777);
}

I haven't yet found a way to check to see what the current directory is.. (not the full path, which is what I did find.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花坊 2024-11-12 05:42:31

如果您有 Boost,则可以使用 Boost.Filesystem

namespace fs = boost::filesystem;

fs::path configFolder(Config::CONFIG_FOLDER_NAME);

// Check if the current directory isn't already config folder
if (!fs::equivalent(configFolder, fs::current_path())
{
    // Create config folder if it doesn't exist
    if (!fs::exists(configFolder))
       fs::create_directory(configFolder);

    // Change working directory to config folder
    fs::current_path(configFolder);
}

顺便说一句,如果您只是计划读取配置文件,则不需要更改工作目录。直接读取即可,使用绝对路径。在 Boost.Filesystem 中,您可以这样做:

fs::path configFilePath = configFolder;
configFilePath /= "config.file";

// read configFilePath

If you have Boost, you can use Boost.Filesystem:

namespace fs = boost::filesystem;

fs::path configFolder(Config::CONFIG_FOLDER_NAME);

// Check if the current directory isn't already config folder
if (!fs::equivalent(configFolder, fs::current_path())
{
    // Create config folder if it doesn't exist
    if (!fs::exists(configFolder))
       fs::create_directory(configFolder);

    // Change working directory to config folder
    fs::current_path(configFolder);
}

By the way, if you're just planning to read a config file, you shouldn't need to change the working directory. Just read directly, using an absolute path. In Boost.Filesystem, you'd do that this way:

fs::path configFilePath = configFolder;
configFilePath /= "config.file";

// read configFilePath
唐婉 2024-11-12 05:42:31

如果您有完整路径,则只需解析它并查看最后一段(最后一个“/”之后的部分)。

If you have the full path, then just parse it and look at the last segment (the part after the last "/").

傲世九天 2024-11-12 05:42:31

我认为 getcwd() 正是您所需要的。

I think getcwd() is what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文