Archived

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

Nazmir

Problems with fstream

Recommended Posts

When I''m using fstream inside a constructor in one of my classes, the files open and everything is great, until I close the program that is. I get a memory read error after I close it. If I open the file inside of a member function, everything is fine, but I don''t get any text writing to the file. Does anyone know what is wrong with my program. Nazmir

Share this post


Link to post
Share on other sites
When I''m using fstream inside a constructor in one of my classes, the files open and everything is great, until I close the program that is. I get a memory read error after I close it. If I open the file inside of a member function, everything is fine, but I don''t get any text writing to the file. Does anyone know what is wrong with my program.

I don''t know why you are getting this error, because I have done the same in one of my prjects and it did work.

So it would be helpful if you can post the routine where you do your file work.

Windows (N): A 32 Bit patch to a 16 bit graphical interface based on a 8 bit operating system originaly encoded for a 4 bit processor written by a 2 bit company that can''''t stand 1 bit of competition.

Share this post


Link to post
Share on other sites
Well just a quick couple of comments that I hope our obvious:

1) make sure your closing the stream when your done (maybe in a function call or the deconstructor)

2) also what are you calling to open the file? it should be something like :
my_file.open("myfile.txt", ios::in | ios::out);

make sure you saying ios::out of course...

anyways, if I said it once, i''ll say it a million times: please when you have a problem, specify the problem, then find out some possible problems or solutions with it, then ask us and tell us whay you did !



Pactuul

Share this post


Link to post
Share on other sites
quote:
Original post by Pactuul
Well just a quick couple of comments that I hope our obvious:

1) make sure your closing the stream when your done (maybe in a function call or the deconstructor)



You don''t have to. This C++. the destructor of fstream will take care of that without you worrying about it.

Share this post


Link to post
Share on other sites
quote:
Original post by Pactuul
true...

I just like to think that it''s good coding pratice



Yes and no, because I think another good practice is to not write code you don''t need

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes, but you do need to close the file as soon as you are done with it. Otherwise you lose whatever is still in the buffer if your program ever has a crash, or is terminated without benefit of exiting gracefully.

To reply to the original poster, make sure that your fstream objects are scoped in your entire class, and not declared in your constructor.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Yes, but you do need to close the file as soon as you are done with it. Otherwise you lose whatever is still in the buffer if your program ever has a crash, or is terminated without benefit of exiting gracefully.

No, you just need to flush it, like so: myStream << flush;
Closing the file will do that for you as a side-effect, but there''s no need to close it if you don''t need to.

Share this post


Link to post
Share on other sites