Jump to content
  • Advertisement
Sign in to follow this  
m4gnus

std::ofstream decl. in class doesn't work

This topic is 4514 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

I noticed a strange problem. I have a (recursive) function that writes a tree to a file but i have to declare the std::ofstream object in the function(which doesn't work properly in a recursive function) or nothing gets written to the file. I tried declaring the object in a class and at the top of the cpp file and both resulted in an empty file... If you want some code i'll post it but the text describes pretty exactly what my code does. nothing more in there. So does anyone has an idea what could cause that problem? regards, m4gnus

Share this post


Link to post
Share on other sites
Advertisement
ok here's the function that (should) writes my tree to the file:

void ngLog::writeNodeToFile(ngLogNode *node)
{

//it works if i declare the std::ofstream object here but if i declare it somewhere other than in this function the file stays empty
std::ofstream out2;
out2.open(filename.c_str(),std::ios_base::out|std::ios_base::in|std::ios_base::app);
//out2<<"test";
out2<<"<"<<node->getName()<<">";
std::vector<ngLogNode*> children=node->getChildren();
int size=children.size();
for(std::vector<ngLogNode*>::iterator it=children.begin();it!=children.end();it++)
writeNodeToFile((*it));

out2<<"</"<<node->getName()<<">";

}



regards,
m4gnus

Share this post


Link to post
Share on other sites
How about this.

void ngLog::writeNodeToFile(ngLogNode *node)
{
std::ofstream out2(filename.c_str());
writeNode(node, out2);
}

void ngLog::writeNode(ngLogNode* node, std::ostream& ostr)
{
ostr << "<" << node->getName() << ">\n";
std::vector<ngLogNode*> children=node->getChildren();
for(std::vector<ngLogNode*>::iterator it=children.begin();it!=children.end();it++)
writeNode(*it, ostr);

ostr << "</" << node->getName() << ">\n";
}


Share this post


Link to post
Share on other sites
Question: What happens to the std::ofstream when it's declared in the function that doesn't happen to it when it's declared in the class?

Answer: It goes out of scope at the end of the function - so it gets flushed.

Your problem is probably caused by this. A std::ofstream member variable will only get destroyed when the owning class gets destroyed. This may not happen under some circumstances which you need to avoid.

The stream won't be destroyed properly when it's a member variable if:

- The class is a derived class and the base class does not have a virtual destructor
- You created the class dynamically (new, new[] or placement new) but you never destroy it (delete, delete[], explicit destructor call)
- Your class is held by an std::auto_ptr/boost::scoped_ptr AND the class was not a comlete type when the smart pointer was declared (i.e. you used a forward declaration and compiler generated or inline destructor).
- Your program exits abnormally (crashes).

Share this post


Link to post
Share on other sites
@Bremage ok that works thank you
@Nitage:
Ah thank you for clarifying what happened

regards,
m4gnus

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!