mkdir c++ 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10402499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:01:08  来源:igfitidea点击:

mkdir c++ function

c++visual-studio-2008

提问by Bruce

I need to use the mkdir c++ function in VS 2008 which takes two arguments and is deprecated from VS 2005.

我需要在 VS 2008 中使用 mkdir c++ 函数,它需要两个参数,并且从 VS 2005 中被弃用。

However this function is used in our code and I need to write a standalone product (containing only mkdir function) to debug something.

但是,我们的代码中使用了此函数,我需要编写一个独立的产品(仅包含 mkdir 函数)来调试某些内容。

What header files do I need to import? I used direct.h, however compiler complains that the argument does not take 2 arguments (reason for this is the function was deprecated in VS 2005).

我需要导入哪些头文件?我使用了 direct.h,但是编译器抱怨该参数没有采用 2 个参数(原因是该函数在 VS 2005 中已被弃用)。

mkdir("C:\hello",0);

回答by

If you want to write cross-platform code, you can use boost::filesystemroutines

如果要编写跨平台代码,可以使用boost::filesystem例程

#include <boost/filesystem.hpp>
boost::filesystem::create_directory("dirname");

This does add a library dependency but chances are you are going to use other filesystem routines as well and boost::filesystemhas some great interfaces for that.

这确实添加了一个库依赖项,但您可能也会使用其他文件系统例程,并且boost::filesystem有一些很好的接口。

If you only need to make a new directory and if you are only going to use VS 2008, you can use _mkdir()as others have noted.

如果你只需要创建一个新目录,如果你只打算使用 VS 2008,你可以_mkdir()像其他人说的那样使用。

回答by Michael Goldshteyn

It's deprecated, but the ISO C++ conformant _mkdir()replaced it, so use that version. All you need to call it is the directory name, its sole argument:

它已被弃用,但符合 ISO C++ 标准的_mkdir()替代了它,因此请使用该版本。你只需要调用它的目录名称,它的唯一参数:

#include <direct.h>

void foo()
{
  _mkdir("C:\hello"); // Notice the double backslash, since backslashes 
                       // need to be escaped
}

Here is the prototype from MSDN:

这是来自MSDN的原型:

int _mkdir( const char *dirname );

int _mkdir( const char *dirname );

回答by Francesco Noferi

My cross-platform solution (recursive):

我的跨平台解决方案(递归):

#include <sstream>
#include <sys/stat.h>

// for windows mkdir
#ifdef _WIN32
#include <direct.h>
#endif

namespace utils
{
    /**
     * Checks if a folder exists
     * @param foldername path to the folder to check.
     * @return true if the folder exists, false otherwise.
     */
    bool folder_exists(std::string foldername)
    {
        struct stat st;
        stat(foldername.c_str(), &st);
        return st.st_mode & S_IFDIR;
    }

    /**
     * Portable wrapper for mkdir. Internally used by mkdir()
     * @param[in] path the full path of the directory to create.
     * @return zero on success, otherwise -1.
     */
    int _mkdir(const char *path)
    {
    #ifdef _WIN32
        return ::_mkdir(path);
    #else
    #if _POSIX_C_SOURCE
        return ::mkdir(path);
    #else
        return ::mkdir(path, 0755); // not sure if this works on mac
    #endif
    #endif
    }

    /**
     * Recursive, portable wrapper for mkdir.
     * @param[in] path the full path of the directory to create.
     * @return zero on success, otherwise -1.
     */
    int mkdir(const char *path)
    {
        std::string current_level = "";
        std::string level;
        std::stringstream ss(path);

        // split path using slash as a separator
        while (std::getline(ss, level, '/'))
        {
            current_level += level; // append folder to the current level

            // create current level
            if (!folder_exists(current_level) && _mkdir(current_level.c_str()) != 0)
                return -1;

            current_level += "/"; // don't forget to append a slash
        }

        return 0;
    }
}

回答by Electro

Nowadays there is the _mkdir()function.

现在有这个_mkdir()功能。