在C ++中创建文件夹 [英] Creating folders in C++

查看:55
本文介绍了在C ++中创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用C ++进行工作,遇到这种情况,当我在执行代码时必须创建目录时.当我必须创建一个文件夹时,代码工作正常,但是当我必须使用此新创建的文件夹创建另一个文件夹时,该代码将失败.

I have recently started working in C++ and came across this situation when I have to create a directory while executing my code. The code is working fine when I have to create a single folder but it fails when I have to create another folder withing this newly created folder.

假设我在 C:中,并且想将我的文件存储在 C:/A/B/中.下面的代码使用 mkdir()可以正常工作,如果我必须将文件存储在 C:/A/中,但是在添加另一个文件夹 B 时失败.

Suppose, I am in C: and want to store my file in C:/A/B/ .The following piece of code using mkdir() works fine if I have to store my file in C:/A/ but fails when I am adding another folder B.

以下是我的代码段:

#include <sys/stat.h>
#include <string>
using namespace std;

int main()
{
    string stringpath = "C:/A/B/"; 
    int status = mkdir(stringpath.c_str(),0777);

    if(status!=0)
    {
        //.....
    }
    else
    {
        //....
    }
}

有人可以帮助我创建此目录,在该目录中父目录中可以有任意数量的文件夹吗?(PS:我已经添加了头文件sys/stat.h,iostream和字符串)

Can someone help me in creating this directory where I can have any number of folders inside the parent directory? (P.S:I have added the header files sys/stat.h,iostream and string)

推荐答案

这是在C ++ 17中的操作方式:

This is how you do it in C++17:

#include <filesystem>
namespace fs = std::filesystem;
fs::create_directories("./a/b/c")

这篇关于在C ++中创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆