• Advertisement

Archived

This topic is now archived and is closed to further replies.

incomplete ofstream data written to file

This topic is 4972 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's my problem. I declare an ofstream object named debugtxt as static in one of my headers that's inherited in most .cpp files. It's supposed to be a log file.
static ofstream debugtxt("debug.txt");
  
Then, throughout the program I have strings that are supposed to be passed out to debug.txt. In each game object, for instance. CGameObject is the base class and tempor_object is an inherited one. Each constructor and destructor has a little string to write, however, my file output is this:
Loading Init
Loading Image
tempor_CGameObject Destructor
or_object Destructor
  
On the third line, only part of the tempor constructor string is written and nowhere in sight is the CGObject's constructor. The CGObject's destructor string is shown however. On the fourth line, only part of the string is displayed for the tempor destructor. Each line written ends with an endl. The file is closed in my main engine management class' destructor. Any clues? Edit: Just as an aside, I'm using VC6 and closing with alt+f4. -Hyatus "da da da" [edited by - Hyatus on June 8, 2004 5:18:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
The order of initialization of global objects is undefined. Chances are that the CObject constructor is being called before the constructor for your debugtxt ofstream.

To address this issue, try to find a comprehensive article on Singletons. (Or alternately a book with a good discussion.)

Share this post


Link to post
Share on other sites
Thanks SiCrane.
That was exactly it.

I''ll post my quick little singleton class just in case anyone
else has this trouble down the line.

I''ll have to wait til I get home, though.
Thanks again. ^_^

-Hyatus
"da da da"

Share this post


Link to post
Share on other sites
Okay, I had some help from a flipcode page.
That link is here.

The code I used for my cheap little log class is:


//Log.h

class Log
{
public:
static Log & GetInstance(){static Log instance;return instance;};
ofstream debugtxt;
~Log(){debugtxt.close();};
private:
Log(){debugtxt.open("debug.txt");};
};


You then write to the log file like so:

Log::GetInstance().debugtxt << "Whatever." << endl;


Thanks again SiCrane and thanks to Xavier on FlipCode.

-Hyatus
"da da da"

Share this post


Link to post
Share on other sites

  • Advertisement