Jump to content
  • Advertisement
Sign in to follow this  
nprz

headaches with fstream

This topic is 4841 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 finally got to the point of logging games in my online poker game, except there is one thing holding me back. Whenever I try to write to the file, the bad bit gets set. The strange thing (and I hope someone can help me with this) is that if I use a local variable for the file handler, it outputs fine, but if I use one for the class, it fails. If I get desparate enough, I will just open the file up locally each time and use append, but I don't see why there is a problem with storing the ofstream variable as a member of a class. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
That's a bit vague so the answer will be as well. The following works:


#include

struct a {
a()
: f( "test.txt" ) {
}

std::ofstream f;
};

int
main() {
a x;
x.f

Placing an fstream as a member of a class doesn't make it non-functional. Make sure you've actually opened the stream that's a member.


// ville

Share this post


Link to post
Share on other sites
I would post all my code, but it would be intermangled with over 1100 lines of code.

It is kind of like ville's code, except I load the file in another function and I use the variable inside member functions.

For the time being I will just reopen the file whenever I need to write to it.

Share this post


Link to post
Share on other sites
Do you reuse the stream? Is it possible you need to close/reopen it somewhere?

That said, there probably isn't any good design *reason* to have the stream as a member. Class data members exist logically to represent part of the object, and physically to allow data to be stored and accessed across function calls on the object (it's another level of scoping between 'function' and 'global'). Neither is normally applicable for file streams.

Oh, it sounds like you want to continually write stuff during the lifetime of the object, and then finish it all up at the end of the object's scope? Please, more details on what the class in question is and what you're trying to do :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Do you reuse the stream? Is it possible you need to close/reopen it somewhere?

That said, there probably isn't any good design *reason* to have the stream as a member. Class data members exist logically to represent part of the object, and physically to allow data to be stored and accessed across function calls on the object (it's another level of scoping between 'function' and 'global'). Neither is normally applicable for file streams.

Oh, it sounds like you want to continually write stuff during the lifetime of the object, and then finish it all up at the end of the object's scope? Please, more details on what the class in question is and what you're trying to do :)


I reuse it a lot, I open it each time a game is created at a particular table and close it when the game ends, etc.

It is part of the table class and logs the actions of different functions (i.e. if a player checks/calls/raises it logs that to that file). Since there are multiple tables in the game, I wanted each table to have it's own file stream.

The file stream is only used by that class, so it seemed logical to put it there rather than somewhere else.

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!