如果目录不存在则创建

发布于 2025-01-04 11:48:16 字数 515 浏览 1 评论 0原文

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

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

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

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

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.docx
but I want him to create the test folder if it doesn't exist.

I want to do that without using the Boost library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

千と千尋 2025-01-11 11:48:16

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

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

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

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

string(OutputFolder+CopiedFile).c_str()

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

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

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

You can use this function without checking if the directory already exists as it will fail but GetLastError() will return 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:

string(OutputFolder+"\\"+CopiedFile).c_str()
十雾 2025-01-11 11:48:16
#include <experimental/filesystem> // or #include <filesystem> for C++17 and up
    
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
}
#include <experimental/filesystem> // or #include <filesystem> for C++17 and up
    
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
}
夜空下最亮的亮点 2025-01-11 11:48:16

也许最简单、最有效的方法是使用 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 - 文档

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.

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

护你周全 2025-01-11 11:48:16

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

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

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


CreateFolder("C:\\folder_name\\")

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

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.

凑诗 2025-01-11 11:48:16

_mkdir 也可以完成这项工作。

_mkdir("D:\\test");

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

_mkdir will also do the job.

_mkdir("D:\\test");

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

放我走吧 2025-01-11 11:48:16

从 c++17 开始,您可以轻松地执行此跨平台操作:

#include <filesystem>
int main() {
bool created_new_directory = false;
bool there_was_an_exception = false;

try {
  created_new_directory
      = std::filesystem::create_directory("directory_name");
} catch(std::exception & e){
there_was_an_exception = true;
// creation failed
}
if ((not created_new_directory) and (not there_was_an_exception)) {
    // no failure, but the directory was already present.
  }
}

注意,如果您需要知道该目录是否实际上是新创建的,则此版本非常有用。
我发现 cppreference 的文档在这一点上有点难以理解:如果目录已经存在,则此函数返回 false。

这意味着,您可以使用此方法或多或少地自动创建一个新目录。

Since c++17, you can easily do this cross-platform with:

#include <filesystem>
int main() {
bool created_new_directory = false;
bool there_was_an_exception = false;

try {
  created_new_directory
      = std::filesystem::create_directory("directory_name");
} catch(std::exception & e){
there_was_an_exception = true;
// creation failed
}
if ((not created_new_directory) and (not there_was_an_exception)) {
    // no failure, but the directory was already present.
  }
}

Note, that this version is very useful, if you need to know, whether the directory is actually newly created.
And I find the documentation on cppreference slightly difficult to understand on this point: If the directory is already present, this function returns false.

This means, you can more or less atomically create a new directory with this method.

假装不在乎 2025-01-11 11:48:16

特定于 OpenCV

Opencv 支持文件系统,可能是通过其依赖项 Boost 实现的。

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

OpenCV Specific

Opencv supports filesystem, probably through its dependency Boost.

#include <opencv2/core/utils/filesystem.hpp>
cv::utils::fs::createDirectory(outputDir);
初相遇 2025-01-11 11:48:16

这适用于 GCC:

取自:
在 C 中创建新目录

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat st = {0};

if (stat("/some/directory", &st) == -1) {
    mkdir("/some/directory", 0700);
}

This works in GCC:

Taken from:
Creating a new directory in C

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

struct stat st = {0};

if (stat("/some/directory", &st) == -1) {
    mkdir("/some/directory", 0700);
}
裸钻 2025-01-11 11:48:16

使用 CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);< /a>

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

Use CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);

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

节枝 2025-01-11 11:48:16

您可以使用cstdlib

尽管 - http://www.cplusplus.com/ articles/j3wTURfi/

#include <cstdlib>

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

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

#include <dirent.h>

You can use cstdlib

Although- 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文