简体   繁体   English

在 C++ 中创建目录

[英]Create directory in C++

I'm making a simple game in DirectX and C++.我正在用 DirectX 和 C++ 制作一个简单的游戏。 But I want to create a directory in Documents for the settings etc.但我想在 Documents 中为设置等创建一个目录。

But I don't know how I need to do this?但我不知道我需要怎么做?

Can someone help me?有人能帮我吗?

The C++11 standard n3337 does not know about directories . C++11 标准n3337不知道目录 C++17 has <filesystem> standard header. C++17 有<filesystem>标准头文件。

If your implementation is C++17 compliant (it probably is not), use std::filesystem::create_directory如果您的实现符合 C++17(可能不是),请使用std::filesystem::create_directory

Otherwise, use operating system primitives.否则,使用操作系统原语。 Be aware that the notion of directory can be OS specific (ie different in one OS from another, perhaps even between different file systems ).请注意,目录的概念可以是特定于操作系统的(即在一个操作系统中与另一个操作系统不同,甚至可能在不同的文件系统之间)。 For Windows, study the WinAPI (so consider CreateDirectoryA ).对于 Windows,研究WinAPI (所以考虑CreateDirectoryA )。 For Linux, look into its syscalls(2) (so consider mkdir(2) )对于 Linux,查看它的syscalls(2) (所以考虑mkdir(2)

Some frameworks, notably Qt , POCO , Boost , provide common wrappers above them.一些框架,特别是QtPOCOBoost ,在它们之上提供了通用包装器。

您可以使用<direct.h>的简单 mkdir 创建目录

_mkdir("C:\\Data\\FolderName");

You can use Visual Studio's <filesystem>你可以使用 Visual Studio 的<filesystem>

There is a function create_directory that has the signature有一个具有签名的函数create_directory

template<class Path>
inline bool create_directory(
   const Path& Pval
);

You can find the user's Documents directory usingSHGetKnownFolderPath which has the signature您可以使用具有签名的SHGetKnownFolderPath找到用户的 Documents 目录

HRESULT SHGetKnownFolderPath(
  _In_     REFKNOWNFOLDERID rfid,
  _In_     DWORD            dwFlags,
  _In_opt_ HANDLE           hToken,
  _Out_    PWSTR            *ppszPath
);

In this case the REFKNOWNFOLDERID you want to use isFOLDERID_Documents在这种情况下, REFKNOWNFOLDERID您要使用的FOLDERID_Documents

Note this is specific to Visual Studio.请注意,这是特定于 Visual Studio 的。 The C++ <filesystem> library is still in the works (ie experimental) for now, but hopefully is coming soon! C++ <filesystem>库目前仍在开发中(即实验性的),但希望很快就会推出!

If you don't have C++17 yet and look for a platform agnostic solution, use ghc::filesystem .如果您还没有 C++17 并寻找与平台无关的解决方案,请使用ghc::filesystem The header-ony code is compatible to C++17 (in fact a backport) and easy to migrate later on. header-ony 代码与 C++17 兼容(实际上是向后移植)并且以后易于迁移。

对于视觉工作室:

System::IO::Directory::CreateDirectory(yourDir);

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

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