Jump to content
  • Advertisement

Archived

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

Carradine

problem with <fstream>

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

This sounds simple but this problem completely baffles me. All I am trying to do is open a file for output. If I do something simple like this ofstream GenInfoOut; GenInfoOut.open(fName,ios::binary); It will open a "new" file (it will erase the current file located there, and set it back to a size of 0). I want to open a file for output so I can rewrite data back into the file. This whole situation works perfectly with "fstream.h", but not . I cannot figure out this simple problem. Any help would be appreciated. Carradine

Share this post


Link to post
Share on other sites
Advertisement
I have, however when I define ios::app, it cannot use seek() command to reposition the cursor, the ios::app seems to override everything i try to reposition that cursor within the file. I can only append, but I need to randomly access file positions.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
remembering an old project...try opening with the flags ios::in | ios::out | ios::binary. i think that will allow you to reposition the file pointer and write data. to seek to the end, try seekg(0, ios::end) (i think that''s 0 bytes from the end of file marker). i haven''t tried this yet, but it might work.

Share this post


Link to post
Share on other sites
nope, that will not work either, if fact i cannot open a file using boith flags.

What I have just noticed is this: I have a ofstream object, i open a file with it (it creates a file and sets it to 0 size), then i open other files with the same object. But I come back to the original file later, and is DOESNT resize the file back to 0 again, I have full access to it in fact.

However, if i open that file with a new ofstream object, it will reset that file back to 0 size and erase the data.

I am inclined to go back to fstream.h again, this is insane.

Share this post


Link to post
Share on other sites
Instead of using an ofstream, I use the fstream object and it works for me. The ofstream open() function defaults to the ios::trunc flag which erases everything in the file... try this..

#include <fstream>
using namespace std;
....
fstream file;
file.open( filename.c_str(), ios::in | ios::out | ios::binary );

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
isn''t the ''o'' in ofstream ''output''?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yes you would have to use fstream instead of ofstream. i think by default ofstream writes ascii. using fstream you should be able to specify ios::in | ios::out

Share this post


Link to post
Share on other sites
Okay, now i have a problem just accessing the same file more then once, even after closing and re-opening the file.

if(MeshInfoIn.is_open())
{
MeshInfoIn.close();
}

MeshInfoIn.open(*meshIt,std::ios::binary | std::ios::in);
if(MeshInfoIn.fail())
int x = 0;

After doing this the second time, the file fails to open and I dont know how to determine what is the problem. I just try to close the file, then open it again.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!