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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:06:44+00:00 2026-05-23T03:06:44+00:00

I have written C++ code for performing calculations. There is a loop in the

  • 0

I have written C++ code for performing calculations. There is a loop in the code. At the end of each loop, I want to:

1) Get the time, a calculation result.

2) Make a name for a file. The name should contain the time.

3) Print the file name into an external file. Each new loop should overwrite the file name from the previous loop.

The first problem I had was that I could not delete the OLD file name. So when my calculation was finished, the name was (for example):
calculationForRestartFile_0.0005476490.004925880.01763170.04375820

instead of:
calculationForRestartFile_04375820

I have updated this question to incorporate Mat’s advice. Thanks Mat. But now I’m not getting anything in the external file. Can anyone see where I’m going wrong? I would be very grateful for any advice.

// Above loop:
  std::string  filename = "calculationForRestartFile_";  // Part of the file name that ALL files should have
  ofstream fileNameAtHighestTimeStream;    

  std::string       convertedToString;                  // This and the line below:
  std::stringstream storeNumberForConversion;           // For storing a loop number/time as a string

// Inside loop:
    storeNumberForConversion << global_time << flush;       // Turn the time/loop number into a string that can be added to the file name for a particular loop
    convertedToString = storeNumberForConversion.str();

    fileNameAtHighestTimeStream.open ("externalFile", ios::out | ios::app ); 
    fileNameAtHighestTimeStream << filename << convertedToString << endl;    // Append the time/loop name to the file name and write to the external file
    fileNameAtHighestTimeStream.close();

// End loop
  • 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-23T03:06:44+00:00Added an answer on May 23, 2026 at 3:06 am

    The issue is that this line is adding to your stringstream inside the loop. You are never resetting its contents.

    storeNumberForConversion << global_time << flush;
    

    The simplest thing to do is to move the declaration of storeNumberForConversion inside your loop so it is created empty just before you use it.

    Alternatively you could reset it after your formatting operation.

    storeNumberForConversion.str( std::string() );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code for performing calculations. There is a loop in the code.
I have written code like this. <img id='test_img' src='../../..' /> I want to get
To use code I have written for performing calculations, I need to read in
I have written a few modules of code in Access vba. Each code runs
I have the following code written to make my lazy second CPU core working.
I have written code that automatically creates CSS sprites based on the IMG tags
I have written some code in my VB.NET application to send an HTML e-mail
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
Here's a snippet of code from a shell script I have written: for src
Put it another way: what code have you written that cannot fail. I'm interested

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.