ここには、フォルダまたはファイルが存在するかどうかを判断するための非常に基本的なコンソールプログラムがあります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に切り替えて、GnuGCCでコンパイルしています。私はそれが簡単な修正だと確信しています-誰かが助けることができますか?
(明らかに私はこれに慣れていないので、私が省略した他の情報が必要な場合は私に知らせてください)。
アップデート
問題はベースディレクトリにありました。更新された動作中のプログラムは次のとおりです。
#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;
}
別の更新
パスの末尾のバックスラッシュは大きな問題であることが判明しました。