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

  • SEARCH
  • Home
  • 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 658445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:56:04+00:00 2026-05-13T22:56:04+00:00

I have this code to open multiple files one at a time that is

  • 0

I have this code to open multiple files one at a time that is given at the command line, and then if it cannot open one of the files, it closes all the files and exits.

/* Opens an array of files and returns a pointer to the first 
 * element (the first file).
 */
ifstream *OpenFiles(char * const fileNames[], size_t count)
{
    /* If no command line arguments, error and exit */
    if (count == 0) {
        cerr << "Invalid number of arguments.";
        exit(EXIT_FAILURE);
    }
    ifstream *fileObj;
    fileObj = new ifstream[count];

    if (fileObj == NULL) {
        cerr << "Failed to create space for files";
        exit(EXIT_FAILURE);
    }

    /* Opens one file at a time and closes all files if there is an 
     * error opening any file.
     */
    for (int loopCount = 0; loopCount < (int)count; loopCount++) { 
        fileObj[loopCount].open(fileNames[loopCount], ios::out); 
        if (!fileObj[loopCount].is_open()) { 
            cerr << "Failed to open " << fileNames[loopCount] << "\n";
            for (; loopCount >= 0; loopCount--) {
                fileObj[loopCount].close();
                cout << "Closed " << fileNames[loopCount] << "\n";
            }
            delete[] fileObj;
        } 
    }
    return fileObj;
}

I am doing this for homework and my teacher has another checker we have to submit to and gives me these types of warnings:

Assign8_1.cpp(44): error 445: (Warning -- Reuse of for loop variable 'loopCount' at 'line 40' could cause chaos)
    return fileObj;
Assign8_1.cpp(51): error 850: (Info -- for loop index variable 'loopCount' whose type category is 'integral' is modified in body of the for loop that began at 'line 40')
    return fileObj;
Assign8_1.cpp(51): error 449: (Warning -- Pointer variable 'fileObj' previously deallocated [Reference: file Assign8_1.cpp: lines 30, 48])
Assign8_1.cpp(30): error 831: (Info -- Reference cited in prior message)
Assign8_1.cpp(48): error 831: (Info -- Reference cited in prior message)
}
Assign8_1.cpp(63): error 818: (Info -- Pointer parameter 'files' (line 55) could be declared as pointing to const)
}

Starting with the first warning, I was wondering why I shouldn’t use my loopCount variable twice the way I do in my code. That was the way I thought it would work, keeping track of which file I am looking at, opening, and closing it appropriately.

Does anyone know what error 449 means? Thanks.

  • 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-13T22:56:05+00:00Added an answer on May 13, 2026 at 10:56 pm

    You need to exit(EXIT_FAILURE) after you delete[] fileObj in the loop, otherwise you’ll simply crash on the next iteration. That may be what warning 449 is telling you.

    Other than that, the code looks fine. If you want it to compile without those warnings, though, you could turn the inner loop into a standard for-loop which only uses loopCount as a bound. Something like:

    for (int i = loopCount; i >= 0; i--) {
        fileObj[i].close();
        cout << "Closed " << fileNames[i] << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer >>> def getitem(context, key): try: return context[key] except KeyError: pass… May 14, 2026 at 10:58 pm
  • Editorial Team
    Editorial Team added an answer It is connected with the issue described at Did I… May 14, 2026 at 10:58 pm
  • Editorial Team
    Editorial Team added an answer You cannot add a Global.asax to a winform application in… May 14, 2026 at 10:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.