简体   繁体   English

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

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

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 以某种方式创建目录吗?

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
}

Update: Since C++17, <filesystem> is the portable way to go.更新:从 C++17 开始, <filesystem>是可移植的方式。 For earlier compilers, check out Boost.Filesystem .对于早期的编译器,请查看Boost.Filesystem


The header you are linking to is effectively turning your (POSIX) dirent.h calls into (native) Windows calls.您链接到的标头有效地将您的 (POSIX) dirent.h调用转换为(本机)Windows 调用。 But dirent.h is about dir ectory ent ries, ie reading directories, not creating ones.但是dirent.hDIR ectory耳鼻喉科里斯,即目录,而不是创造者。

If you want to create a 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一个类似的包装标头将您的 (POSIX) mkdir()调用转换为相应的(本机)Windows 函数调用(我无法为您指出这样的标头),或
  • use the Windows API directly, which might be pragmatic but would lead to a really ugly mix of POSIX and Windows functions...直接使用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.h etc., allowing you to use the POSIX API consistently in your programming.另一种解决方案是查看Cygwin ,它提供了一个在 Windows 上运行的 POSIX 环境,包括 Bash shell、GCC 编译器工具链和一个完整的 POSIX 头文件集合,如dirent.hsys/stat.hsys/types.h等,允许您在编程中一致地使用 POSIX API。

Visual Studio includes the <direct.h> header. Visual Studio 包含<direct.h>标头。 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.此标头声明_mkdir 和 _wmkdir ,它们可用于创建目录,并且是 Visual Studio 附带的 C 库的一部分。

The other "easy" option would be to use Windows API calls as indicated by DevSolar .另一个“简单”选项是使用DevSolar指示的 Windows API 调用。

You can use sys/types.h header file and use您可以使用 sys/types.h 头文件并使用
mkdir(const char*) method to create a directory mkdir(const char*)创建目录的方法
Following is the sample code以下是示例代码

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

mkdir is deprecated. mkdir 已弃用。 Give #include <direct.h> as a header file.#include <direct.h>作为头文件。 then write然后写

_mkdir("C:/folder")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM