我想列出遞歸給出文件路徑的所有文件和子目錄。這工作,直到當我嘗試添加代碼來檢查文件路徑是可讀/可寫的(我評論了線)。它現在不會進入遞歸循環。這是我的代碼檢查文件路徑是否可讀可寫
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
void listDir(char *name, FILE *fp)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
if (!(entry = readdir(dir)))
return;
do {
FILE *fileCopy;
char read[50];
char write[50];
char path[1024];
int len = snprintf(path, sizeof(path)-1, "%s/%s", name, entry->d_name);
path[len] = 0;
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
// if((fileCopy = fopen(path, "rb")) == NULL){
// strcpy(read,"Not Readable");
// }
// else{
// strcpy(read,"Readable");
// }
// if((fileCopy = fopen(path, "wb")) == NULL){
// strcpy(write,"Not Writable");
// }
// else{
// strcpy(write,"Writable");
// }
fprintf(fp,"[D]%s - %s,%s\n", path,read,write);
listDir(path ,fp);
}
else
{
// if((fileCopy = fopen(path, "rb")) == NULL){
// strcpy(read,"Not Readable");
// }
// else{
// strcpy(read,"Readable");
// }
// if((fileCopy = fopen(path, "wb")) == NULL){
// strcpy(write,"Not Writable");
// }
// else{
// strcpy(write,"Writable");
// }
fprintf(fp,"[F]%s - %s,%s\n", path,read,write);
}
} while ((entry = readdir(dir)));
closedir(dir);
}
int main(void)
{
FILE *fp;
fp = fopen("/var/mobile/Applications/FileIOAccess.txt", "w");
listDir("/var",fp);
fclose(fp);
return 0;
}
'(進入== READDIR(DIR)'???? – Recker
我相信它不是錯誤,但ANW試了一下,現在有文件 – user2541163
你被分配值進入這是在沒有輸出錯誤的'while'循環結構... [This](http://linux.die.net/man/3/readdir)可能會有幫助...嘗試根據「返回值」部分中的信息更改條件。 .. – Recker