如何查找文件夹是否存在以及如何创建文件夹
How to find out if a folder exists and how to create a folder?
我试图创建一个文件夹,如果它不存在。我正在使用Windows,我对我的代码在其他平台上工作不感兴趣。
没关系,我找到解决办法了。我只是有包容问题。答案是:#include <io.h> // For access().
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
#include <iostream>
#include <string>
using namespace std;
string strPath;
cout << "Enter directory to check: ";
cin >> strPath;
if ( access( strPath.c_str(), 0 ) == 0 )
{
struct stat status;
stat( strPath.c_str(), &status );
if ( status.st_mode & S_IFDIR )
{
cout << "The directory exists." << endl;
}
else
{
cout << "The path you entered is a file." << endl;
}
}
else
{
cout << "Path doesn't exist." << endl;
}
posix兼容的调用是mkdir
。当目录已经存在时,它会静默失败。
如果您使用的是Windows API,那么CreateDirectory
更合适。
使用boost::filesystem::exists
检查文件是否存在
boost::filesystem::create_directories
就是这样做的:给它一个路径,它将在该路径中创建所有缺失的目录。
相关文章:
- 如何在文档文件夹中创建目录
- 如何从Windows控制台调用.exe(C++)以在不同的目录(或任何目录)中创建文件夹?
- 如何使用Windows API C++更改已创建的文件夹/目录安全权限
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- 在c++编程中的文件夹中创建文件
- 创建文件夹结构
- 在C++中创建文件夹
- 如何使用 c++ 创建对"everyone"具有共享访问权限的文件夹
- 如何在 C++ 中创建一个适用于 Windwos 和 Linux 的文件夹(目录)
- Windows 7:MFC ActiveX 控件不会在任何文件夹中创建文件
- Qt在文件夹之间创建链接
- inotify 的替代方法,用于检测何时在文件夹下创建新文件
- Qt Creator在指定的调试文件夹中创建调试和发布文件夹
- 在源代码所在文件夹以外的其他文件夹中创建文件
- 想要本地驱动器上的文件,但是它总是在项目文件夹中创建
- 为什么在不同的文件夹上创建 EXE 和 PDB 文件
- 无法在资产文件夹中创建.db文件
- 在不同的文件夹中创建文件和链接库
- 如何在桌面上的文件夹中创建文本文件
- 在Arduino草图文件夹中创建库