我在这里有一个非常基本的控制台程序,以确定文件夹或文件是否存在或不使用stat
:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
char path[] = "myfolder/";
struct stat status;
if(stat(path,&status)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
我也试过这个access
版本:
#include <iostream>
#include <io.h>
using namespace std;
int main() {
char path[] = "myfolder/";
if(access(path,0)==0) { cout << "Folder found." << endl; }
else { cout << "Can't find folder." << endl; } //Doesn't exist
cin.get();
return 0;
}
他们都没有找到我的文件夹(与程序位于同一目录中)。这些适用于我的最后一个编译器(DevCpp 的默认编译器)。如果有帮助,我现在切换到 CodeBlocks 并使用 Gnu GCC 进行编译。我确定这是一个快速修复 - 有人可以帮忙吗?
(显然我是个菜鸟,所以如果您需要我遗漏的任何其他信息,请告诉我)。
更新
问题出在基本目录上。更新后的工作程序如下:
#include <iostream>
#include <sys/stat.h>
using namespace std;
int main() {
cout << "Current directory: " << system("cd") << endl;
char path[] = "./bin/Release/myfolder";
struct stat status;
if(stat(path,&status)==0) { cout << "Directory found." << endl; }
else { cout << "Can't find directory." << endl; } //Doesn't exist
cin.get();
return 0;
}
另一个更新
事实证明,路径上的尾随反斜杠是个大麻烦。