ディレクトリとそのサブディレクトリ内のすべてのファイルとフォルダの名前を印刷しようとしています。 ファイルはディレクトリとも呼ばれるという問題がありますか?私のコード全体を投稿して申し訳ありませんが、なぜこれが起こっているのかわかりません。ありがとうございました。あなたはopendir()
で開かれたディレクトリにstat()
を呼んでいるS_ISDIRはファイルがディレクトリだと思っています
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
void parcurgere(char path[1000]){
char aux[1000];
strcpy(aux, path);
struct dirent *ent;
DIR *d = opendir(path);
while((ent = readdir(d))){
struct stat my_stat;
stat(path, &my_stat);
if(S_ISDIR(my_stat.st_mode) && strcmp(ent->d_name, ".")!=0 && strcmp(ent->d_name, "..")!=0 && strcmp(ent->d_name, ".DS_Store")!=0){
printf("%s - folder\n", ent->d_name);
char x[1000];
strcpy(x, path);
strcat(x, "/");
strcat(x, ent->d_name);
parcurgere(x);
}
if(S_ISREG(my_stat.st_mode)){
printf("%s - fisier\n", ent->d_name);
}
strcpy(path, aux);
//path[strlen(path) - poz] = '\0';
}
}
int main(){
char path[1000];
strcpy(path, ".");
parcurgere(path);
return 0;
}
@MichaelWalz私はそれをしました。それは最初の場合です。 –
@RadLexusフォルダ構造/ツリーを意味しますか? –
デバッガでコードを実行すると、すぐにこの問題を見つけることができます。私はここにlinuxを持っていないので、私は確認できません。 –