Is there a way using the standard c or c++ library to make a directory, including the subfolders that may be required given a string of the absolute path?
有没有办法使用标准的c或c ++库来创建一个目录,包括给定一个绝对路径字符串可能需要的子文件夹?
Thanks
3 个解决方案
#1
7
No, however if you're willing to use boost:
不,但是如果你愿意使用boost:
boost::filesystem::path dir("absolute_path");
boost::filesystem::create_directory(dir);
There is a proposal to add a filesystem library to the standard library that will be based on boost::filesystem
. Using boost::filesystem
and appropriate typedefs will put you in a good position to migrate to the future standard when it becomes available for your compiler of choice.
有人建议将文件系统库添加到基于boost :: filesystem的标准库中。使用boost :: filesystem和相应的typedef将使您能够在选择的编译器可用时迁移到将来的标准。
#2
8
Using the standard library, you'd do it like so in C++:
使用标准库,你可以在C ++中这样做:
// ASSUMED INCLUDES
// #include <string> // required for std::string
// #include <sys/types.h> // required for stat.h
// #include <sys/stat.h> // no clue why required -- man pages say so
std::string sPath = "/tmp/test";
mode_t nMode = 0733; // UNIX style permissions
int nError = 0;
#if defined(_WIN32)
nError = _mkdir(sPath.c_str()); // can be used on Windows
#else
nError = mkdir(sPath.c_str(),nMode); // can be used on non-Windows
#endif
if (nError != 0) {
// handle your error here
}
#3
4
Yes, In C++17, You can use filesystem
是的,在C ++ 17中,您可以使用文件系统
#include <filesystem>
#if __cplusplus < 201703L // If the version of C++ is less than 17
// It was still in the experimental:: namespace
namespace fs = std::experimental::filesystem;
#else
namespace fs = std::filesystem;
#endif
int main()
{
// create multiple directories/sub-directories.
fs::create_directories("SO/1/2/a");
// create only one directory.
fs::create_directory("SO/1/2/b");
// remove the directory "SO/1/2/a".
fs::remove("SO/1/2/a");
// remove "SO/2" with all its sub-directories.
fs::remove_all("SO/2");
}
Note to use only forward slashes /
and you may need to include <experimental/filesystem>
.
注意只使用正斜杠/您可能需要包含
#1
7
No, however if you're willing to use boost:
不,但是如果你愿意使用boost:
boost::filesystem::path dir("absolute_path");
boost::filesystem::create_directory(dir);
There is a proposal to add a filesystem library to the standard library that will be based on boost::filesystem
. Using boost::filesystem
and appropriate typedefs will put you in a good position to migrate to the future standard when it becomes available for your compiler of choice.
有人建议将文件系统库添加到基于boost :: filesystem的标准库中。使用boost :: filesystem和相应的typedef将使您能够在选择的编译器可用时迁移到将来的标准。
#2
8
Using the standard library, you'd do it like so in C++:
使用标准库,你可以在C ++中这样做:
// ASSUMED INCLUDES
// #include <string> // required for std::string
// #include <sys/types.h> // required for stat.h
// #include <sys/stat.h> // no clue why required -- man pages say so
std::string sPath = "/tmp/test";
mode_t nMode = 0733; // UNIX style permissions
int nError = 0;
#if defined(_WIN32)
nError = _mkdir(sPath.c_str()); // can be used on Windows
#else
nError = mkdir(sPath.c_str(),nMode); // can be used on non-Windows
#endif
if (nError != 0) {
// handle your error here
}
#3
4
Yes, In C++17, You can use filesystem
是的,在C ++ 17中,您可以使用文件系统
#include <filesystem>
#if __cplusplus < 201703L // If the version of C++ is less than 17
// It was still in the experimental:: namespace
namespace fs = std::experimental::filesystem;
#else
namespace fs = std::filesystem;
#endif
int main()
{
// create multiple directories/sub-directories.
fs::create_directories("SO/1/2/a");
// create only one directory.
fs::create_directory("SO/1/2/b");
// remove the directory "SO/1/2/a".
fs::remove("SO/1/2/a");
// remove "SO/2" with all its sub-directories.
fs::remove_all("SO/2");
}
Note to use only forward slashes /
and you may need to include <experimental/filesystem>
.
注意只使用正斜杠/您可能需要包含