2012-01-24 3 views
1

부스트를 사용하여 디렉토리 내용을 나열하고 각 파일을 반복하며 데이터 처리 작업을 수행하는 코드가 있습니다. 결과는 출력 파일 ('histFile')에 인쇄됩니다."열린 파일이 너무 많습니다"

boost::filesystem::directory_iterator::construct: Too many open files: "/Users/.../.../.../directory_with_files"

내 코드는 다음과 같습니다 : ~ 2555 개 파일이 처리 된 후, 나는 오류가

for(int i = 0; i < 10000; i++) { 
    FILE *histFile; 
    string outputFileName = "somename"; 
    bool ifRet = initFile(histFile, outputFileName.c_str(), "a"); // 1 
    fclose(histFile);            // 2 
} 

나는 위의 마지막 두 줄을 주석 경우 ('1'과 '2') , 코드는 잘 끝납니다. 따라서 그것은 'histFile'의 복사본이 열려있는 상태로 남아있는 것처럼 보이지만 어떻게 이해할 수 없습니다! 이것은 메소드의 작동 부분입니다.

bool initFile(FILE *&ofFile, const char *fileName, const char *openType, int overwriteOption) { 

if(overwriteOption < 0 || overwriteOption > 2) { 
    fprintf(stderr, "ERROR: ToolBox - initFile() : unknown 'overwriteOption' (%d), setting to (0)!\n", overwriteOption); 
} 

// Read-Only 
if(openType == "r") { 
    if(ofFile = fopen(fileName, "r")) { return true; } 
    fprintf(stderr, "ERROR: Could not open file (%s)!\n", fileName); 
    return false; 
} 

// Appending: 
if(openType == "a" || openType == "a+") { 
    // Check if file already exists 
    if(!fopen(fileName, "r")){ 
     fprintf(stderr, "ERROR: (%s) File does not Exist, cannot append!\n", fileName); 
     return false; 
    } 
    if(ofFile = fopen(fileName, openType)) { return true; }  
} 

// Writing: 
// if file already exists 
if(FILE *temp = fopen(fileName, "r")){ 
    if(overwriteOption == 2) { 
     fprintf(stderr, "ERROR: (%s) File Exists!\n", fileName); 
     return false; 
    } 
    if(overwriteOption == 1) { 

    } 
    if(overwriteOption == 0) { 
     char backupFileName[TB_CHARLIMIT], backupPrefix[TB_CHARLIMIT]; 
     strcpy(backupFileName, fileName);         // copy filename 
     // create a prefix w/ format '<YYYYMMDD>BACKUP_' 
     DateTime now; 
     sprintf(backupPrefix, "%s", now.getDateStr().c_str()); 
     strcat(backupPrefix, "BACKUP_"); 
     // add to copied filename, and move file 
     strcpy(backupFileName, prependFileName(backupFileName, backupPrefix)); 
     moveFile(fileName, backupFileName); 
    } 
    fclose(temp); 
} 

if(ofFile = fopen(fileName, openType)) { return true; } 


// Default: Return error and false 
fprintf(stderr, "ERROR: Could not open file (%s)!\n", fileName); 
return false; 
} 

포인터/참조에 문제가 있습니까? 도움을 주시면 감사하겠습니다! 그 확인 할 수있는 이유는 정말

// Appending: 
if(openType == "a" || openType == "a+") { 
    // Check if file already exists 

    if(!fopen(fileName, "r")){  // <-- the FILE* opened here is leaked 

     fprintf(stderr, "ERROR: (%s) File does not Exist, cannot append!\n", fileName); 
     return false; 
    } 
    if(ofFile = fopen(fileName, openType)) { return true; }  
} 

있습니까 : 파일이 이미 존재하는 경우 테스트 할 때

+3

게시 한 코드에 문제가 있다는 것을 말하고 있습니까? 나는 거기에 어떤 향상을 볼 수 없기 때문에 boost :: filesystem이 문제와 관련이있다? –

+0

아마도'initFile'의 나머지 부분을 보여줄 필요가 있습니다 - 나는 그것이 열려있는 파일을 유출하는 예외를 던지고있는 것 같습니다. –

+0

@ Paur R : 예외가 발생하면 나머지 루프는 실행되지 않습니다. 그래서 그렇게 될 수는 없습니다. – TonyK

답변

4

당신은 코드의이 비트에 핸들을 새는거야? 왜 파일이 ​​존재하지 않는다면 파일을 만들지 않을까요?

+0

고마워! 그게 전부 야. '진짜'이유는 없다. 그냥 청결. – DilithiumMatrix

관련 문제