2009-10-27 2 views
1

는 특정 파일이 프로그램에 의해 나열되지 않은 이유를 알고 있습니까 그들은 "일반적인"경우에도?이 프로그램을 실행 한 후목록 일반 파일 유일한 문제 (디렉토리없이)

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/param.h> 
#include <sys/stat.h> 
#include <dirent.h> 

int main(void) { 
    DIR *dh = opendir("./"); // directory handle 
    struct dirent *file; // a 'directory entity' AKA file  
    struct stat info; // info about the file. 
    while (file = readdir(dh)) { 
    stat(file->d_name, &info); 
    printf("note: file->d_name => %s\n", file->d_name); 
    printf("note: info.st_mode => %i\n", info.st_mode); 
    if (S_ISREG(info.st_mode)) 
     printf("REGULAR FILE FOUND! %s\n", file->d_name); 
    } 
    closedir(dh); 

    return 0; 
} 

, 나는이 얻을 :

note: file->d_name => . 
note: info.st_mode => 16877 
note: file->d_name => .. 
note: info.st_mode => 16832 
note: file->d_name => .DS_Store 
note: info.st_mode => 16832 
note: file->d_name => ef efeff 
note: info.st_mode => 16832 
note: file->d_name => ffffff 
note: info.st_mode => 16832 
note: file->d_name => ffffff - copie 
note: info.st_mode => 16832 
note: file->d_name => folder 
note: info.st_mode => 16832 
note: file->d_name => printiie.tt 
note: info.st_mode => 16832 
note: file->d_name => test.c 
note: info.st_mode => 33188 
REGULAR FILE FOUND! test.c 
note: file->d_name => z 
note: info.st_mode => 33188 
REGULAR FILE FOUND! z 

본 그림에서 알 수있는 것처럼 프로그램에는 두 개의 파일 만 있습니다. 그러나 모든 파일은 규칙적이고 하나의 폴더 만 있습니다. 여기

는 쉘 명령의 사본의 과거입니다 : $ ls -lai :

total 64 
2421444 drwxr-xr-x 10 denis staff 340 27 oct 22:19 . 
2416789 [email protected] 28 denis staff 952 27 oct 22:20 .. 
2423204 [email protected] 1 denis staff 6148 27 oct 21:41 .DS_Store 
2423206 [email protected] 1 denis staff 895 27 oct 19:57 ef efeff 
2423183 [email protected] 1 denis staff 895 27 oct 19:57 ffffff 
2423216 [email protected] 1 denis staff 895 27 oct 19:57 ffffff - copie 
2423436 drwxr-xr-x 2 denis staff 68 27 oct 21:57 folder 
2423180 [email protected] 1 denis staff 38 27 oct 21:32 printiie.tt 
2423682 [email protected] 1 denis staff 895 27 oct 19:57 test.c 
2423208 [email protected] 1 denis staff 34 27 oct 21:39 z 

난 그냥 각 파일의 이름을 나열하고 싶지만, 디렉토리없이. Mac OS X에서 작동하지만 문제의 원인이 될 수 있다고 생각하지 않습니다.

답변

5

일부 파일에 대해 stat 함수가 실패한 것처럼 보이므로 info 구조체가 업데이트되지 않고 그 안에있는 데이터가 여전히 ".."입니다. 그 라인을 다음으로 대체하십시오 :

if (stat(file->d_name, &info)) 
{ 
    printf("error: stat(%s): %s\n", file->d_name, strerror(errno)); 
    continue; 
} 

그리고 그 이유를 알 수 있습니다.

+0

감사! 나열되지 않은 파일에 대해이 메시지가 표시됩니다. 오류 : stat (printiie.tt) : 해당 파일이나 디렉토리가 없습니다. 그 이유는 이해할 수없는 순간입니다 ... – Denis

+1

디렉토리와 비슷한 모양입니다. 'opendir '로 검사하는 것은'stat'가보고있는 현재 작업 디렉토리와 다릅니다. 대신'opendir (".")'이 어떻게됩니까? – caf

+0

몇 가지 테스트를 마친 후 다음과 같은 메시지가 나타납니다. note : file-> d_name =>. 참고 : info.st_mode => 16877 note : 파일 -> d_name => .. 참고 : info.st_mode => 16832 .DS_Store를 삭제하면 프로그램이 0 파일을 검색하므로 이상합니다. – Denis

-2

한때 모든 yml 파일을 하위 디렉토리를 포함한 디렉토리에 나열해야했습니다. 그것은 승 XP에서 : system("dir C:\Path\To\File\*.yml /B /S >> list.txt"); 그리고 나서 나는 그들 모두를 얻기 위해 list.txt를 분석 할 것입니다. 한 줄은 하나의 파일이었습니다. 쉽지만 휴대용은 아닙니다. 또한 그것은 약간의 시간 전이었다 - 지금 나는 아마 부스트를 시험해 보일 것이다.

편집 : 이 대답은 코드에 대한 것이 아니라 :

Actually I just would like to list the name of each file, but without directories.

날, 그는 자신의 문제가 해결 될 것 같은 그 같은 접근 방식을했다합니다.

+1

-1 : 코드에 대한 질문에 대한 대답이 아닙니다. –