Sign in to follow this  
m4gnus

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

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this