Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi ,

I have declared one static fstream file under header.h
static ofstream myfile_logs("D:\\Elec...");

main.cpp
C++
#include "header.h"
  
 int main(...)
 {
 cal_mem();
 verify_data();
 }
  
 verify_data()
 {
 myfile_logs << "verify...";
 ...
 }
 //end of main .cpp
  
 commonFunc.cpp
 #include "header.h"
  
 void cal_mem()
 {
 myfile_logs << "calculate Mem";
 ...
 ..
 }
 //end of cal_mem

When i run this program, on opening the log file
i get only

"calculate Mem" text displayed
the control does come to verify_data but the file is not appended or updated with verify.. text
The log file seems to work only for commonFunc.cpp
how to have this log file appended for multiple cpps?

Quite urgent to fix this

Thanks
Anna
Posted
v2
Comments
Richard MacCutchan 20-Mar-13 5:17am    
You also posted this question in the C++ forum; please post in one place only so we do not duplicate effort and waste our time. You can waste as much of yours as you like.

A possible solution :) :
C++
// header.h
class logger
{
public:
  static ofstream& GetStream()
  {
    static ofstream myfile_logs("D:\\Elec...");
    return myfile_logs;
  }
};

// any.cpp
void test()
{
  logger::GetStream() << "test\n";
}


...else - you can also use "external" declarations, instead of a static in the header :)
 
Share this answer
 
Comments
anne_rose 20-Mar-13 3:52am    
Thanks a lot Eugen...It worked . and how abt static variables? I have declared two static var(a1 and a2) under same header and then i have to keep incrementing the variables by 1 till the prog end. Once the commonFunc.cpp terminates, the variable a1 is reset to 0.
Eugen Podsypalnikov 20-Mar-13 4:04am    
Probably in the same manner :)
class twoGlobalUints
{
static UINT& GetFirst()
{
static UINT uiFirst(0);
return uiFirst;
}
static UINT& GetSecond()
{
static UINT uiSecond(0);
return uiSecond;
}
};
// any.cpp
UINT test()
{
twoGlobalUints::GetFirst() = twoGlobalUints::GetSecond() = 11;
twoGlobalUints::GetFirst()++;
return twoGlobalUints::GetFirst() - twoGlobalUints::GetSecond();
}
anne_rose 20-Mar-13 4:51am    
Thanks a lot. Its perfect.
The keyword static in C and C++ has (within others) the meaning of being file local. Hence, it is not a good idea to put such a declaration in a header file. That would just create multiple independent objects, one for each file into which you include the header.

Instead, you should define the stream in your main.cpp (or any other of your cpp files) and pass a pointer or reference around to the other files.

As Eugen already pointed out, you could put an extern declaration in the header file to accomplish that.

[EDIT: Example]
C++
// in header.h
extern std::ofstream myfile_logs;
    // this just declares a global variable, the definition of which
    // resides in .cpp


C++
// in main.cpp
std::ofstream myfile_logs ("D:\\Elec...");
    // this defines a global variable shared among all your .cpp files

should just work fine.
 
Share this answer
 
v2
Comments
anne_rose 20-Mar-13 4:04am    
I tried extern keyword but that resulted in linkage problem and on replacing with static it solved the linkage prob. Working on visual studio 2012. So still recommended to use extern?
nv3 20-Mar-13 4:28am    
Yes, extern should definitely work for you. I have ammended my solution to show you the syntax. See above.
anne_rose 20-Mar-13 5:16am    
I tried it and it worked!!!!!
In main.cpp i never incl keyword "std"::ofstream myfile...and now both soln working perfectly. Thank you so much . Thank u all for the time and effort .
nv3 20-Mar-13 5:43am    
You are very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900