Problems with fstream

Started by
9 comments, last by Nazmir 22 years, 5 months ago
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
Advertisement
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.
--------------------------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.
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
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
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.

true...

I just like to think that it''s good coding pratice
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
true...

I just like to think that it''s good coding pratice
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
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
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.
If you are getting a memory error, also make sure that you do not delete the fstrem object and then try to use the pointer to the object after its deleted.
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.

This topic is closed to new replies.

Advertisement