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
  • 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-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 currently have a small Java program which I would like to run both
in a C program I have an long* that I want to serialize (thus
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I am writing a piece of code in Java that needs to take a
Most program languages have some kind of exception handling; some languages have return codes,
In my program I have one array with 25 double values 0.04 When I
Okay here's the program I have typed up(stdio.h is included also): /* function main
I want to turn a program I have into a service so I can

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.