Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:09:00+00:00 2026-05-28T13:09:00+00:00

I have code using boost to list directory contents, iterate through each file, and

  • 0

I have code using boost to list directory contents, iterate through each file, and do some data processing stuff. The results are being printed to an output file (‘histFile’).
After ~2555 files have been processed, I get the error:

boost::filesystem::directory_iterator::construct: Too many open files: “/Users/…/…/…/directory_with_files”

My code is:

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

If I comment out the last two lines above (‘1’ and ‘2’), the code finishes fine. Thus it seems copies of ‘histFile’ are being left open, but I don’t understand how! This is the operative part of the method:

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;
} 

Am I doing something wrong with pointers/references?
Any help greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:09:01+00:00Added an answer on May 28, 2026 at 1:09 pm

    You’re leaking a handle in this bit of code when you’re testing if the file exists already:

    // 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; }     
    }
    

    Is there really a reason to make that check? Why not just let the file be created if it doesn’t already exist?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some trouble reading from an ini file using boost program options. The
Greetings all. I am writing some code using the Boost Units library and have
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
We have started using the boost unit testing library for a large existing code
I have some code using a System.Transactions.TransactionScope , that creating a new instance of
I have some code using Lucene that leaves the default conjunction operator as OR,
I have some code using libxml2's SAX2 interface. I want to be able to
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
I have the following code to test memory deallocation using a std::list container: #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.