Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ mkdir c++;功能_C++_Visual Studio 2008 - Fatal编程技术网

C++ mkdir c++;功能

C++ mkdir c++;功能,c++,visual-studio-2008,C++,Visual Studio 2008,我需要在VS 2008中使用MKDIR C++函数,它使用两个参数,而不是VS 2005。p> 然而,我们的代码中使用了这个函数,我需要编写一个独立的产品(只包含mkdir函数)来调试一些东西 我需要导入哪些头文件?我使用了direct.h,但编译器抱怨该参数不包含2个参数(原因是该函数在VS2005中被弃用) 它被禁止,但是ISOC++一致性 .MKDIR()/C++ >替换它,所以使用那个版本。您只需调用目录名,它是唯一的参数: #include <direct.h> void

我需要在VS 2008中使用MKDIR C++函数,它使用两个参数,而不是VS 2005。p> 然而,我们的代码中使用了这个函数,我需要编写一个独立的产品(只包含mkdir函数)来调试一些东西

我需要导入哪些头文件?我使用了direct.h,但编译器抱怨该参数不包含2个参数(原因是该函数在VS2005中被弃用)


它被禁止,但是ISOC++一致性<代码> .MKDIR()/C++ >替换它,所以使用那个版本。您只需调用目录名,它是唯一的参数:

#include <direct.h>

void foo()
{
  _mkdir("C:\\hello"); // Notice the double backslash, since backslashes 
                       // need to be escaped
}
#包括
void foo()
{
_mkdir(“C:\\hello”);//注意双反斜杠,因为反斜杠
//需要逃走吗
}
以下是来自以下方面的原型:

int_mkdir(const char*dirname)


现在有了这个函数。

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

#include <boost/filesystem.hpp>
boost::filesystem::create_directory("dirname");
#包括
boost::filesystem::create_目录(“dirname”);
这确实增加了一个库依赖项,但很可能您还将使用其他文件系统例程,
boost::filesystem
有一些很好的接口来实现这一点


如果您只需要创建一个新目录,并且只打算使用VS 2008,您可以使用其他人提到的
\u mkdir()

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

#包括
#包括
//适用于windows mkdir
#ifdef_WIN32
#包括
#恩迪夫
命名空间utils
{
/**
*检查文件夹是否存在
*@param foldername要检查的文件夹的路径。
*@如果文件夹存在,则返回true,否则返回false。
*/
bool文件夹_存在(std::string foldername)
{
结构统计;
stat(foldername.c_str(),&st);
返回st.st_模式&S_IFDIR;
}
/**
*mkdir()内部使用的mkdir的可移植包装器
*@param[in]path要创建的目录的完整路径。
*@成功返回零,否则返回-1。
*/
int_mkdir(常量字符*路径)
{
#ifdef_WIN32
返回::_mkdir(路径);
#否则
#if_POSIX_C_源
return::mkdir(路径);
#否则
return::mkdir(路径0755);//不确定这在mac上是否有效
#恩迪夫
#恩迪夫
}
/**
*mkdir的递归、可移植包装器。
*@param[in]path要创建的目录的完整路径。
*@成功返回零,否则返回-1。
*/
int mkdir(常量字符*路径)
{
std::字符串当前_级别=”;
std::字符串级别;
std::stringstream ss(路径);
//使用斜杠作为分隔符拆分路径
while(std::getline(ss,level,“/”)
{
当前_level+=level;//将文件夹追加到当前级别
//创建当前级别
如果(!folder_存在(当前_级别)&&&_mkdir(当前_级别.c_str())!=0)
返回-1;
当前_级别+=“/”;//不要忘记附加斜杠
}
返回0;
}
}

ISO C++不是跨平台的吗?为什么要在这里添加boost依赖项?我不打算-1或任何东西,但这太过分了。为什么只为添加目录而添加链接时间库依赖项?Boost文件系统不仅仅是头文件,我避免使用文件系统相关的函数,因为它们大多是特定于系统/编译器的。我不确定关于<代码> MKDIR()/<代码>,但你能指给我一个引用,在这里定义为标准的ISO C++ C++ @ MikelGoStyyn:从什么时候开始<代码>?在标准中找不到它。所以你要么依赖于编译器,要么依赖于boost库,后者似乎更可取。我相信前面的下划线就是MS states所做的,而不是意义<代码> .MKDIr 本身是ISO C++标准的成员。当一个简单的<代码> >如果“MyScViver ”定义MKDIR(x)MKDIR(x)< /代码>就足够了。调用stat函数时,应该检查返回代码是否有错误。如果返回-1,则出现错误。在VisualStudio2010上(至少),如果文件夹不存在,函数将返回-1,并且所有标志都将设置为1。我建议您这样编辑:int ret=stat(dirPath.c_str(),&st);返回(ret==0)和(st.st\U模式和S\U IFDIR)?真:假;这是正确的。有关C++17中的现代方法,请参见以下答案:
#include <boost/filesystem.hpp>
boost::filesystem::create_directory("dirname");
#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;
    }
}