C++ 在 windows 的 dirent.h 中找不到 mkdir() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23427804/
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-28 00:26:25  来源:igfitidea点击:

Can't find mkdir() function in dirent.h for windows

c++cvisual-c++mkdirdirent.h

提问by Rakesh Malik

I am using dirent.h 1.20 (source) for windows in VC2013.

我在 VC2013 中为 windows使用 dirent.h 1.20 ( source)。

I can't find mkdir()in it.

mkdir()在里面找不到。

How am I supposed to use it? Or can I create a directory somehow only using dirent.h?

我该如何使用它?或者我可以仅使用 dirent.h 以某种方式创建目录吗?

采纳答案by DevSolar

Update:Since C++17, <filesystem>is the portable way to go. For earlier compilers, check out Boost.Filesystem.

更新:从 C++17 开始,<filesystem>是可移植的方式。对于早期的编译器,请查看Boost.Filesystem



The header you are linking to is effectively turning your (POSIX) dirent.hcalls into (native) Windows calls. But dirent.his about directory entries, i.e. readingdirectories, not creatingones.

您链接到的标头有效地将您的 (POSIX)dirent.h调用转换为(本机)Windows 调用。但是,dirent.h大约是DIRectory耳鼻喉科里斯,即目录,而不是创造者。

If you want to createa directory (mkdir()), you need either:

如果要创建目录 ( mkdir()),则需要:

  • A similar wrapping header turning your (POSIX) mkdir()call into the corresponding (native) Windows function calls (and I cannot point out such a header for you), or
  • use the Windows APIdirectly, which might be pragmatic but would lead to a really ugly mix of POSIX and Windows functions...
  • 一个类似的包装标头将您的 (POSIX)mkdir()调用转换为相应的(本机)Windows 函数调用(我无法为您指出这样的标头),或
  • 直接使用Windows API,这可能是实用的,但会导致 POSIX 和 Windows 函数的非常丑陋的混合......


// UGLY - these two don't belong in the same source...
#include <dirent.h>
#include <windows.h>

// ...
CreateDirectory( "D:\TestDir", NULL );
// ...


Another solution would be to take a look at Cygwin, which provides a POSIX environment running on Windows, including Bash shell, GCC compiler toolchain, and a complete collection of POSIX headers like dirent.h, sys/stat.h, sys/types.hetc., allowing you to use the POSIX API consistently in your programming.

另一种解决办法是看看Cygwin的,它提供了一个POSIX环境,在Windows上运行,包括Bash shell中,GCC编译器工具链,以及POSIX的头文件的完整集合喜欢dirent.hsys/stat.hsys/types.h等,让您在持续使用POSIX API你的编程。

回答by Rakesh Malik

simplest way that helped without using any other library is.

在不使用任何其他库的情况下提供帮助的最简单方法是。

#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#endif

void createDir(string dir) {
#if defined _MSC_VER
    _mkdir(dir.data());
#elif defined __GNUC__
    mkdir(dir.data(), 0777);
#endif
}

回答by crashmstr

Visual Studio includes the <direct.h>header. This header declares _mkdir and _wmkdir, which can be used to create a directory, and are part of the C libraries included with Visual Studio.

Visual Studio 包含<direct.h>标题。此标头声明_mkdir 和 _wmkdir,它们可用于创建目录,并且是 Visual Studio 附带的 C 库的一部分。

The other "easy" option would be to use Windows API calls as indicated by DevSolar.

另一个“简单”选项是使用DevSolar指示的 Windows API 调用。

回答by crashmstr

You can use sys/types.h header file and use
mkdir(const char*)method to create a directory
Following is the sample code

您可以使用 sys/types.h 头文件和使用
mkdir(const char*)方法创建目录
以下是示例代码

#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
    if(!mkdir("C:mydir"))
    {
        printf("File created\n");
    }
    else
        printf("Error\n");
}

回答by shawon

mkdir is deprecated. Give #include <direct.h>as a header file. then write

mkdir 已弃用。给#include <direct.h>一个头文件。然后写

_mkdir("C:/folder")