gpt4 book ai didi

c++ - 如何确定文件夹是否存在以及如何创建文件夹?

转载 作者:IT老高 更新时间:2023-10-28 22:23:58 25 4
gpt4 key购买 nike

如果文件夹不存在,我正在尝试创建它。我使用的是 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 兼容的调用是 mkdirIt当目录已经存在时静默失败。

如果您使用的是 Windows API,那么 CreateDirectory比较合适。

关于c++ - 如何确定文件夹是否存在以及如何创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5621944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com