Sign in to follow this  

C++ std::fstream - how do I resize?

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

hi I'm after a tidy way to write, read and resize data in an open (binary) file. Since std::fstream does the first two it seems the best choice. I'm after something like: std::fstream::put_EOF_here() or std::fstream::set_file_size(std::streampos pos) If it only works when shrinking, that's OK as I plan to append data when enlarging, to prevent putting indeterminate data in the file, and I can do this with std::fstream::write(). thanks.

Share this post


Link to post
Share on other sites
After searching for a while, all I find are people complaining that there isn't a standard way to do this - resize any file - without copying all the data to memory or a new file.

I'm kinda surprised, isn't this a pretty basic obvious file operation?

Is there a reason why this is missing?

Share this post


Link to post
Share on other sites
Quote:
Original post by atetanat
I'm kinda surprised, isn't this a pretty basic obvious file operation?


Indeed, but I wouldn't call it a terribly standard one. It seems to me that most file I/O is implemented as a one-shot transformation: either from file to useful-in-memory-representation, or vicea versa. Simple, crude, but effective in most cases. Of course, this is probably encouraged by the original lack, a "chicken and the egg" type of situation if you will.

In other words, by the time you get up to actually having a good reason to do in-place overwrites and resizes instead of the simplicity of a simple resave, you're in a bit of an exceptional situation when it comes to filesystems, and will probably be using nonstandard OS-specific functionality anyways.

Of course, all this really is, is making excuses for a woefully tiny C++ standard library -- which is in fact microscopic, tiny, and small. There are plenty of things which are arguably basic, obvious, and standard that the C++ Standard Library just downright lacks.

Share this post


Link to post
Share on other sites
That's as inefficient as bozosort - well, not quite, but.

OS-Specific:
windows: bool WINAPI SetEndOfFile(HANDLE hfile);
unix: int ftruncate(int fildes, off_tsize) <-- old C syntax.

both require closing the fstream and reopening.

meh.

I wonder, would they consider adding:

std::fstream &std::fstream::set_eof();
std::fstream &std::fstream::set_eof(std::streampos);

[EDIT]:
This was intended to be posted before the last poster but when I hit reply the 'net wouldn't connect - I logged on later and it autoposted, so this wasn't in reply to MaulingMonkey.



[Edited by - atetanat on August 7, 2007 4:30:21 AM]

Share this post


Link to post
Share on other sites

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