【问题标题】:mkdir c++ functionmkdir c++ 函数
【发布时间】:2012-05-11 06:19:13
【问题描述】:

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

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

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

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

【问题讨论】:

标签: c++ visual-studio-2008


【解决方案1】:

现在有_mkdir() 函数。

【讨论】:

    【解决方案2】:

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

    #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;
        }
    }
    

    【讨论】:

    • 上面代码中folder_exists函数有错误。当您调用 stat 函数时,您应该检查返回码是否有错误。如果它返回-1,则有错误。在 Visual Studio 2010(至少)上,如果文件夹不存在,该函数将返回 -1,并且所有标志都将设置为 1。我建议您进行此编辑: int ret = stat(dirPath.c_str(), &st );返回 (ret == 0) && (st.st_mode & S_IFDIR) ?真假;这可以正常工作。
    【解决方案3】:

    如果要写跨平台代码,可以使用boost::filesystem套路

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

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

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

    【讨论】:

    • ISO C++ 不是跨平台的?为什么要在此处添加 boost 依赖项?我不会去-1或任何东西,但这是矫枉过正。为什么添加链接时间库依赖项只是为了添加目录? Boost 文件系统不是只是头文件,你知道的。
    • 我避免使用与文件系统相关的函数,因为它们大多是特定于系统/编译器的。我不确定mkdir(),但您能否指出一个将其定义为标准 ISO C++ 的参考?
    • @MichaelGoldshteyn:_mkdir ISO C++ 从什么时候开始?在标准中找不到。因此,您要么依赖于编译器,要么依赖于 boost 库,后者似乎更可取。
    • 我相信前面的下划线是 MS 所说的 "ISO conforming",而不是意味着 _mkdir 本身是 ISO C++ 标准的成员。
    • 真的不明白为什么一个简单的#if _MSC_VER 加上#define mkdir(x) _mkdir(x) 就足够了。
    【解决方案4】:

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

    #include <direct.h>
    
    void foo()
    {
      _mkdir("C:\\hello"); // Notice the double backslash, since backslashes 
                           // need to be escaped
    }
    

    这是来自MSDN的原型:

    int _mkdir( const char *dirname );

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2014-11-10
      • 2013-04-21
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      相关资源
      最近更新 更多