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 6371019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:00:23+00:00 2026-05-25T01:00:23+00:00

In my program I have currently a piece of code that looks like this

  • 0

In my program I have currently a piece of code that looks like this

 void foo()
 {
    // defining local variables

    for (long i =0; i<maxiterations; i++)
    {
       // here the core of the code is executed
       // for each iteration an object of a class is created and modified given the conditions imposed
    }

    if (flag) savedata();

    // and here a call to the destructor of the class is called (tested, it destroys all the previously created objects)

 }

Currently savedata() is like the following

 void savedata()
 {
    char filenameI[1024];
    sprintf_s(filenameI, 1024, "%s_%i", filename, id);
    FILE* File;
    errno_t err;
    err = fopen_s(&File, filenameI, "w");
    if (err!=0)
    {
            cout << "\nFile" << filenameI << "could not be opened.\nPlease press Ctrl+C to terminate" << endl; // the program is run via Matlab
            cin.get();
    }
    else
    {
        cout << "Saving file " << filenameI << endl;
    }

    for (long i =0; i<maxiterations; i++)
    {
        fprintf(File, "%10li", data); //not the actual line, but fprintf is used in this way
    }

    fclose(File);

 }

Since maxiterations is a run-time set long and given the memory required to store a single object is significant (i.e. I need higher values, but I hit the memory limit), I was thinking of modifying the code in the following way:

 void foo()
 {
     // defining local variables
     if (flag) openfile();

     for (long i =0; i<maxiterations; i++)
    {
         // executing the same as before
         if (flag) savedata(i); // obviously the function would be modified
    }

    if (flag) closefile();

 }

Now, finally, my question:

using the same kind of output call (a FILE* instead of a ofstream object), is it possible to achieve what I need?

My doubts arise from the fact that what’s inside a loop has a scope only in that loop and thus I fear that the file could be closed when I exit the first if statement instead of when the closefile() is called.

Am I wrong?

Thanks to anyone who will help.

Federico

  • 1 1 Answer
  • 1 View
  • 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-25T01:00:24+00:00Added an answer on May 25, 2026 at 1:00 am

    suggestion:

    FILE* f = NULL;
    if (flag) f = openfile();
    
     for (long i =0; i<maxiterations; i++)
        {
             // executing the same as before
             if (flag) savedata(i, f); // pass in filehandle, obviously the function would be modified
        }
    
        if (flag) closefile(f); //close file on handle passed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a piece of software that works like this: I have a
I currently have a basic chat program in c++ that uses WinSock2.h with UDP.
I have a python program that I am currently working on which is working
We have an app that currently installs itself into 'program files\our app', and it
I currently have a program written in Cocoa and I would like it to
I have a simple piece of assembly code that works correctly on Mac OS
I have a simple piece of a program thats currently producing some memory leaks
I have a piece of code that checks to see if a macro is
I wrote this piece of code that lists all JPG files in the current
I have this piece of code which gives to SAXParseError i.e., it is not

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.