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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:40:35+00:00 2026-06-08T16:40:35+00:00

Isn’t it possible to open two files simultaneously using different ofstreams? What I am

  • 0

Isn’t it possible to open two files simultaneously using different ofstreams? What I am trying to is writing to two ofstreams, one has a variable filename which changes every time the loop iterates, the other has a fixed filename and the data I am writing on is to be appended at every iteration of the loop. To demonstrate:

ofstream file_variable_name;
ofstream file_to_be_appended;

{  //THIS IS A LOOP, variable_name changes at every iteration

file_variable_name.open(variable_name.c_str(), ios::out);
file_to_be_appended.open("fixed name", ios::out | ios::app);

//Do lots of things here, make data ready to be written to file

file_variable_name << "write something" << endl;
file_to_be_appended << "write same as above, but this is to be appended" << endl;

file_variable_name.close();
file_to_be_appended.close();
}

Somehow, I could not even manage to get the second file to be created let alone opened and appended. I can send the full code (it’s around 1000 lines or so, needs be truncated), as well, but I thought the above would explain what I am trying to do, and any logic flaws would be apparent to pros.

Thanks in advance for all suggestions!

  • 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-06-08T16:40:36+00:00Added an answer on June 8, 2026 at 4:40 pm

    You’re making it far harder than it is. Generally there’s no need to use .open() and .close() methods, and you don’t need to keep reopening the same file:

    #include <fstream>
    
    int main(int argc, char *argv[])
    {
        std::ofstream file_to_be_appended("fixed name");
    
        char const *list[] = {"a", "b", "c"};
        for (auto s : list) { // My compiler doesn't have generalized initializer lists yet
            std::ofstream file_variable_name(s);
    
            file_variable_name << "write something\n";
            file_to_be_appended << "write same as above, but this is to be appended\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Isn't there some way to combine two images together using the xOr operator? I
Isn't there another way of using global variables in a Java stlye manner in
Isn't possible facebook allow us from my apps post or share image to user
This isn't a question that has a black/white yes/no answer, this is more a
Isn't this a client side control? What will happen if the user has disabled
Isn't having all of the files in public view a bad thing? Surely things
Isn't it unnecessary to retain a static variable since it stays around for the
Isn't there an easy way to select the source files and build them with
Isn't possible to retrieve auto lock value? Or anyone know how to modify the
Isn't there any built in class in PHP that has HashTable in PHP? If

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.