[filesystem] how to set a file size?

Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this? Thanks, -- Eric Niebler BoostPro Computing http://www.boostpro.com

Eric Niebler wrote:
Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this?
Thanks,
There are two posix functions ("truncate" and "ftruncate") which might do what you need. HTH Roland

Roland Bock wrote:
Eric Niebler wrote:
Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this?
There are two posix functions ("truncate" and "ftruncate") which might do what you need.
It doesn't look like these functions exist for MSVC users. So it seems there is no portable way to change the size of a file. Beman, would this make a good addition to the filesystem library? -- Eric Niebler BoostPro Computing http://www.boostpro.com

Eric Niebler wrote:
Roland Bock wrote:
Eric Niebler wrote:
Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this?
There are two posix functions ("truncate" and "ftruncate") which might do what you need.
It doesn't look like these functions exist for MSVC users. So it seems there is no portable way to change the size of a file. Beman, would this make a good addition to the filesystem library?
MSVC offers "_chsize_s" for such a task, which (of course), is not available for Linux...

-----Original Message-----
There are two posix functions ("truncate" and "ftruncate") which might do what you need.
It doesn't look like these functions exist for MSVC users. So it seems there is no portable way to change the size of a file. Beman, would this make a good addition to the filesystem library?
MSVC offers "_chsize_s" for such a task, which (of course), is not available for Linux...
The Windows API version for this is called SetEndOfFile. This is a truncating type function. I would use this function unless there is really a need to use the runtime library version. -- Bill --

On Tue, Oct 13, 2009 at 3:26 PM, Eric Niebler
Roland Bock wrote:
Eric Niebler wrote:
Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this?
There are two posix functions ("truncate" and "ftruncate") which might do what you need.
It doesn't look like these functions exist for MSVC users. So it seems there is no portable way to change the size of a file. Beman, would this make a good addition to the filesystem library?
Yes, a truncate function would be a nice addition. It looks doable. I'll look into it further. Thanks, --Beman

Beman Dawes wrote:
On Tue, Oct 13, 2009 at 3:26 PM, Eric Niebler
wrote: Roland Bock wrote:
Eric Niebler wrote:
Forgive me for the n00b-ish (and slightly offtopic) question, but is there any way in standard C++ or in Boost.Filesystem to truncate a file to a particular size? With std::ofstream, I can either truncate a file on open to 0 size, or else open it in overwrite mode and scribble over it, but when I'm done, I don't seem to have a way of saying, "The end of the file is now *here*." Is that right? And if so, does Boost.Filesystem have an API for this?
There are two posix functions ("truncate" and "ftruncate") which might do what you need.
It doesn't look like these functions exist for MSVC users. So it seems there is no portable way to change the size of a file. Beman, would this make a good addition to the filesystem library?
Yes, a truncate function would be a nice addition.
It looks doable. I'll look into it further.
This has now been implemented for Boost.Filesystem V3, with the signature: void file_size( const path& p, uintmax_t size, system::error_code& ec = throws() ); ). See http://svn.boost.org/svn/boost/sandbox/filesystem-v3 I found the POSIX "truncate" name misleading, since on both POSIX and Windows the file can be extended as well as truncated. There is steady progress on V3, so I'm hoping it is ready for a beta in a couple of weeks. --Beman

On Fri, Oct 16, 2009 at 6:40 PM, Scott McMurray
2009/10/16 Beman Dawes
: I found the POSIX "truncate" name misleading, since on both POSIX and Windows the file can be extended as well as truncated.
Agreed, but why size for a mutating function? I'd have expected resize_file or similar.
I agree. file_size seems like a query to me, something like resize_file seems more appropriate.

Scott McMurray wrote:
2009/10/16 Beman Dawes
: I found the POSIX "truncate" name misleading, since on both POSIX and Windows the file can be extended as well as truncated.
Agreed, but why size for a mutating function? I'd have expected resize_file or similar.
The idea was that the getter and setter functions would have the same name. But I like the resize_file name, and will change to that. Thanks, --Beman
participants (6)
-
Beman Dawes
-
Bill Buklis
-
Eric Niebler
-
OvermindDL1
-
Roland Bock
-
Scott McMurray