Sign in to follow this  

writing past a file

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

in my BINARY file im writing some states to file. seekp( position ); write( ... ); this will cause the fstream to go into a failure state if im seeking to a position that is greater than the file size. its like i want the file to grow (in my special case it is failing when i write to position 0 on an empty file). im opening the file and truncating it, ios::trunc. how do i write a specific number of bytes to a binary file stream to a position that could be equal to or larger than the current file size?

Share this post


Link to post
Share on other sites
Well, it shouldn't be failing if you're writing at position 0 when the file is empty. It may just not like you seekp'ing to it (You don't need to seek to 0 right after opening the file).

Is your algorithm always going to potentially generate positions passed the end of the file, or just right at the end?

If you need to jump passed the end of the file, you can always write 0's up until the point you need the file pointer to be at. If you're going to be going back and forth, you'll need to keep track of your maximum position, of course, so that you don't write over what you already wrote.

All in all, though, I would advise that you re-consider your algorithm. I haven't done file I/O extensively, but it seems to me that you shouldn't ever have to jump passed the end of a file to write something. You could probably at least sort your data before writing it to the file, so that everything is in sequence. Of course, I don't know what your doing, so I could be talking rubbish [lol].

Share this post


Link to post
Share on other sites
The use of ios::trunc scares me. Are you using a current compiler with standard C++ (meaning #include <fstream>) and not a prestandard version (meaning #include <fstream.h>) ? Pre-standard C++ means all bets are off and you just hope your implementation happens to work. I sure hope you're using the standard c++ version.

Assuming you opened the file with at least the modes ios_base::trunc | ios_base::out it is completely legal.

Seeking and writing some distance beyond the end of the file is well defined. If it doesn't result in an error (which probably won't happen on a desktop computer), it will result in zeros between the old end of file and the location you seek to.

On operating systems that support it, and if you seek out far enough, it generates a so-called sparse file. An example sparse file would have you seek out a gigabyte, then write a few bytes. The size of the file reported by the OS would be a gigabyte. The actual size on disk would be just a single block.

Share this post


Link to post
Share on other sites

This topic is 4270 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.

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