2014-10-13 3 views
0

안녕하세요 저는 인터넷에서 코드를 섞어서 반복적으로 디렉토리를 검색하는 명령을 만들려고했습니다. 나는 지금 막 붙어있다. 현재 디렉토리 (서브 디렉토리 없음)에 두 개의 파일 만 있고 실제로 출력 된 파일보다 많은 파일이 인쇄 출력에 계속 표시됩니다.처음부터 grep 명령 만들기

entered 
entered 
entered 
enteredIF 
entered 
enteredIF 
entered 
enteredIF 
entered 
enteredIF 

내가 그것을 어떻게해야한다고 생각하는 것은 단지 두 번 인쇄 enteredIF입니다 :

여기 내 인쇄 출력이다. 흥미롭게도, 두 파일에는 11 개의 문자가 있으며 11 개의 인쇄 문이 있습니다. 아마도 이것은 어떻게 든 내 문제와 관련이 있습니다.

아무도 두 개의 인쇄 출력 (각 파일마다 하나씩)을받는 방법을 알려주시겠습니까?

#include <stdio.h> 
#include <unistd.h> 
#include <termios.h> 
#include <dirent.h> 
/* A process is a single process. */ 
typedef struct process 
{ 
    struct process *next;  /* next process in pipeline */ 
    char **argv;    /* for exec */ 
    pid_t pid;     /* process ID */ 
    char completed;    /* true if process has completed */ 
    char stopped;    /* true if process has stopped */ 
    int status;     /* reported status value */ 
} process; 
/* A job is a pipeline of processes. */ 
typedef struct job 
{ 
    struct job *next;   /* next active job */ 
    char *command;    /* command line, used for messages */ 
    process *first_process;  /* list of processes in this job */ 
    pid_t pgid;     /* process group ID */ 
    char notified;    /* true if user told about stopped job */ 
    struct termios tmodes;  /* saved terminal modes */ 
    int stdin, stdout, stderr; /* standard i/o channels */ 
} job; 

/* The active jobs are linked into a list. This is its head. */ 
job *first_job = NULL; 


int main(int argc, char** argv) { 
    // char cwd[1024]; // buffer 
    // char* sdirectory = getcwd(cwd, sizeof(cwd)); 
    // printf("dir name: %s\n", sdirectory); 

    int file_count = 0; 
    DIR* dirp; 
    struct dirent* entry; 

    dirp = opendir("."); 

    while ((entry = readdir(dirp)) != NULL) { 
     printf("entered\n"); 
     if (entry-> d_type == DT_REG) { 
     printf("enteredIF\n"); 
     file_count++; 
    } 

} 
    closedir(dirp); 
    printf("file count: %d\n", file_count); 

은}

+1

입력 한 printf를 다음과 같이 변경하십시오 :'printf ("saw filename '% s'\ n", entry-> d_name);'그러면 어떤 파일 이름을 읽었는지 알 수 있습니다. 아마도'.' 및'..'보다는 점 파일 수가 많을 것입니다. – JohnH

답변

0
는 당신의 입력의 printf를 변경

: printf("saw filename '%s'\n", entry->d_name); 그리고 당신은 그것이 읽고 어떻게 생각하는지는 파일 이름을 볼 수 있습니다. 아마도 ...보다 더 많은 도트 파일을 가지고있을 것입니다. - JohnH