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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:09:22+00:00 2026-06-02T05:09:22+00:00

I am developing a program in c++ with lots of file io operation. I

  • 0

I am developing a program in c++ with lots of file io operation. I have defined a static ofstream in a common header so that it is accessible everywhere in the project. The structure of the codes are list as following: all common variable are defined in com.h, test.h and test.cpp are for a class called OPClass, main.cpp carry the main program

COM.H:

#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>

using namespace std;

static ofstream out;
static stringstream ss;

#endif

TEST.H:

#ifndef __CL__
#define __CL__
#include <iostream>
#include <fstream>
#include "com.h"

using namespace std;

class OPClass
{
  public:
   void run(void);
   void show(ostream &o) const;
};
#endif

TEST.CPP:

#include "com.h"
#include "test.h"

void OPClass::run(void)
{
  out << "Here is run()" << endl;
  show(out);
}

void OPClass::show(ostream &o) const
{
  o << "hello!" << endl;
}

MAIN.CPP:

#include "com.h"
#include "test.h"

void runmain(void)
{
  OPClass op;
  out.open("output.txt", ios::out | ios::trunc);
  out << endl << "State changed!" << endl;
  op.run();
  if (out.is_open()) out.close();
}

int main(int argc, char* argv[])
{
  runmain();
  return  0;
}

As you can see, the static ofstream was named as out and will be called in the main program and in the class. I am using mingw32 and didn’t see any problem on compilation or upon running. But seems that only the information in runmain() will be written to the output file. Any other message written to that file in the class never appear in the output file. Why’s that and how can I written a common file stream so everywhere in the project can access that? 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-06-02T05:09:23+00:00Added an answer on June 2, 2026 at 5:09 am

    Each compilation unit is getting its own ss and out. Hence there is a different instance of them seen by main.cpp than by test.cpp.

    You don’t really need static here. To address this, rather than declaring the variables and their allocations in the header file you need to merely prototype them using the extern keyword.

    #ifndef __CLCOM__
    #define __CLCOM__
    #include <sstream>
    #include <fstream>
    #include <iostream>
    
    // Note: don't put "using" statements in headers
    // use fully qualified names instead 
    extern std::ofstream out;
    extern std::stringstream ss;
    
    #endif
    

    Where you actually put your declarations is up to you, just make sure it’s only in one place. That could be a com.cpp file or you could stick it in main.cpp if that’s appropriate for your project.

    std::ofstream out;
    std::stringstream ss;
    

    Not that global variables like this are a good idea, anyway…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a program, that uses Core Text... And I have a leak
I am developing a program in VB.NET. I have an enum called PriceType that
I'm developing a program that needs to parse the file name into a TTextField
I have been developing a program that needs to be run daily bases. I
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
I'm developing a program that would require huge amount of memory, and I want
I'm developing a program that makes some floating points calculations. Is there any way
I am developing program on window's using msys and mingw. I want to have
Currently am developing a program that solves (if possible) any given labyrinth of dimensions
I'm currently developing a program that will generate reports based upon lead data. My

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.