C++ 如果目录不存在,则创建一个目录

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

Create a directory if it doesn't exist

c++windowsfiledirectorycopy

提问by pourjour

In my app I want to copy a file to the other hard disk so this is my code:

在我的应用程序中,我想将一个文件复制到另一个硬盘,所以这是我的代码:

 #include <windows.h>

using namespace std;

int main(int argc, char* argv[] )
{
    string Input = "C:\Emploi NAm.docx";
    string CopiedFile = "Emploi NAm.docx";
    string OutputFolder = "D:\test";
    CopyFile(Input.c_str(), string(OutputFolder+CopiedFile).c_str(), TRUE);

    return 0;
}

so after executing this, it shows me in the D:HDD a file testEmploi NAm.docxbut I want him to create the test folder if it doesn't exist.

因此,执行此操作后,它会在D:HDD 中向我显示一个文件,testEmploi NAm.docx但我希望他创建测试文件夹(如果该文件夹不存在)。

I want to do that without using the Boost library.

我想在不使用 Boost 库的情况下做到这一点。

回答by hmjd

Use the WINAPI CreateDirectory()function to create a folder.

使用 WINAPICreateDirectory()函数创建文件夹。

You can use this function without checking if the directory already exists as it will fail but GetLastError()will return ERROR_ALREADY_EXISTS:

您可以在不检查目录是否已存在的情况下使用此函数,因为它会失败但GetLastError()会返回ERROR_ALREADY_EXISTS

if (CreateDirectory(OutputFolder.c_str(), NULL) ||
    ERROR_ALREADY_EXISTS == GetLastError())
{
    // CopyFile(...)
}
else
{
     // Failed to create directory.
}

The code for constructing the target file is incorrect:

构建目标文件的代码不正确:

string(OutputFolder+CopiedFile).c_str()

this would produce "D:\testEmploi Nam.docx": there is a missing path separator between the directory and the filename. Example fix:

这会产生"D:\testEmploi Nam.docx":目录和文件名之间缺少路径分隔符。示例修复:

string(OutputFolder+"\"+CopiedFile).c_str()

回答by Chris Rayner

Probably the easiest and most efficient way is to use boost and the boost::filesystem functions. This way you can build a directory simply and ensure that it is platform independent.

可能最简单和最有效的方法是使用 boost 和 boost::filesystem 函数。通过这种方式,您可以简单地构建目录并确保它与平台无关。

const char* path = _filePath.c_str();
boost::filesystem::path dir(path);
if(boost::filesystem::create_directory(dir))
{
    std::cerr<< "Directory Created: "<<_filePath<<std::endl;
}

boost::filesystem::create_directory - documentation

boost::filesystem::create_directory - 文档

回答by Vertexwahn

#include <experimental/filesystem> // or #include <filesystem>

namespace fs = std::experimental::filesystem;


if (!fs::is_directory("src") || !fs::exists("src")) { // Check if src folder exists
    fs::create_directory("src"); // create src folder
}

回答by Fivee Arch

Here is the simple way to create a folder.......

下面是创建文件夹的简单方法......

#include <windows.h>
#include <stdio.h>

void CreateFolder(const char * path)
{   
    if(!CreateDirectory(path ,NULL))
    {
        return;
    }
}


CreateFolder("C:\folder_name\")

This above code works well for me.

上面的代码对我来说效果很好。

回答by jiasli

_mkdirwill also do the job.

_mkdir也会做这份工作。

_mkdir("D:\test");

https://msdn.microsoft.com/en-us/library/2fkk4dzw.aspx

https://msdn.microsoft.com/en-us/library/2fkk4dzw.aspx

回答by saurabheights

OpenCV Specific

OpenCV 特定

Opencv supports filesystem, probably through its dependency Boost.

Opencv 支持文件系统,可能是通过它的依赖 Boost。

#include <opencv2/core/utils/filesystem.hpp>
cv::utils::fs::createDirectory(outputDir);

回答by Noa Yehezkel

You can use cstdlib

您可以使用cstdlib

Although- http://www.cplusplus.com/articles/j3wTURfi/

虽然 - http://www.cplusplus.com/articles/j3wTURfi/

#include <cstdlib>

const int dir= system("mkdir -p foo");
if (dir< 0)
{
     return;
}

you can also check if the directory exists already by using

您还可以使用以下命令检查目录是否已存在

#include <dirent.h>

回答by DotNetUser

Use CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);

CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);

If the function succeeds it returns non-zero otherwise NULL.

如果函数成功,则返回非零,否则返回NULL