2013-02-21 2 views
0

~ 30,000 개의 파일에서 단어를 읽는 프로그램을 작성하고 있지만, 4092 번째 반복 후에는 더 이상 새 파일을 읽을 수 없습니다. 나는 심지어 루프의 끝에서 free (filePointer)를 포함하려고 시도했지만, 4092nd 파일을 열려고 시도한 후에도 내 파일 포인터가 여전히 NULL이었다. 어떤 아이디어 나 제안이라도 대단히 감사하겠습니다.C에서 순차적으로 4092 개 파일 열기

int main(int argc, char *argv[]) { 


    hashTable *bigramHT = create_table(100000000); 
    hashTable *tokenHT = create_table(100000000); 
    int numTokens = 0; 
    /* file pointer */ 
    FILE *fp = NULL; 
    /* directory pointer */ 
    DIR *dirp = NULL; 
    /* pointer to directory struct */ 
    struct dirent *entry = NULL; 

    /* check if directory exists and can be opened */ 
    if((dirp = opendir(argv[1])) == NULL){ 
    return 1; 
    } 

    /* allocate memory for the filename paths (using maximum possible length) */ 
    char *path; 
    path = (char*)malloc((get_longest_filename(dirp) + strlen(argv[1])) * sizeof(char)); 
    if(path == NULL){ 
    return 1; 
    } 

    /* create buffers for 'window' to read in word pairs and initialize to \0 */ 
    char buffer1[1024]; 
    char buffer2[1024]; 
    char wordPair[sizeof(buffer1) + sizeof(buffer2) + 1]; 
    memset(wordPair, '\0', sizeof(wordPair)); 
    memset(buffer1, '\0', sizeof(buffer1)); 
    memset(buffer2, '\0', sizeof(buffer2)); 

    int iterations = 0; 
    rewinddir(dirp); //make sure directory is at start position 
    /* loop through directory */ 
    while((entry = readdir(dirp)) != NULL){ 
    /* make sure filename is not a directory itself */ 
    if(is_dir(entry->d_name) == NULL){ 
     iterations++; 
     /* attempt to open file */ 
     if((fp = fopen(strcat(strcat(strcat(path, argv[1]),"/"), entry->d_name), "r")) == NULL){ 
    printf("file not found: %s after %d iterations\n", entry->d_name, iterations); 
    break; 
    //return 1; 
     } 
     //printf("file: %s\n", entry->d_name); 
     while((fgets(buffer1, sizeof(buffer1), fp)) != NULL){ 
    numTokens++; 
    //puts(remove_newline(buffer1)); 

    if(insert(tokenHT, remove_newline(buffer1)) != 0){ 
     return 1; 
    } 

    if(buffer2[0] != '\0'){ 
     /* merge words into one string */ 
     strcat(strcat(strcpy(wordPair, remove_newline(buffer2)), " "), remove_newline(buffer1)); 
     //printf("%s\n", wordPair); 
     /* insert new string into hash table */ 

     if(insert(bigramHT, wordPair) != 0){ 
     return 1; 
     } 

    } 
    /* shift window */ 
    strcpy(buffer2, buffer1); 
     } 
     strcpy(path, ""); 
     memset(buffer2, '\0', sizeof(buffer2)); 
    } 
    } 

    printf("Total number of tokens = %d\n", numTokens); 
    //print(tokenHT); 
    //print(bigramHT); 
    delete_table(bigramHT); 
    delete_table(tokenHT); 
    closedir(dirp); 
    free(path);; 

    return 0; 
} 

답변

6

너는 fclose(fp);이 없으므로 파일 핸들이 부족합니다.

+1

* face-palm * 감사합니다! – user2052561

관련 문제