파일 경로가 주어진 경우 모든 파일과 하위 디렉토리를 재귀 적으로 나열하려고합니다. 이것은 파일 경로가 읽기/쓰기가 가능한지 (줄을 주석으로 처리했는지) 확인하는 코드를 추가 할 때까지 작동합니다. 이제는 재귀 루프로 들어 가지 않습니다. 이 예는 파일 권한을 테스트 fopen
의 사용을 대체 할 access
를 사용하여 내 코드파일 경로가 읽기 및 쓰기 가능인지 확인
#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
나는이 아니 그 오류가 있지만 순자산은 그것을 시도, 이제 파일 – user2541163
당신이 항목에 값을 할당했다에는 출력이없는 생각 'while' 루핑 구조체에 대해 오류가 있습니다 ... [This] (http://linux.die.net/man/3/readdir)는 도움이 될 수 있습니다 ... 반환 값 섹션의 정보에 따라 조건을 변경하십시오. .. – Recker