我试图创建一个文件夹,如果它不存在。我使用的是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;
}
认真吗?您的第一个问题没有代码片段,并且此代码不反映该问题。这更像是“我的代码有什么问题(我没有发布)?” – 2011-04-11 13:33:12
您应该发布编辑作为答案并接受它。 – 2011-04-11 13:38:48
它没有让我发布它作为答案或评论。 – Sara 2011-04-13 09:42:28