how to truncate a file

J

jkherciueh

Hi,

I am writing a container template file_of< POD > that should resemble
std::vector< T > as closely as feasible given that the container actually
interfaces to an underlying file on disk. E.g., insertion can take
place *only* at the end. Nonetheless, I would like to implement the
pop_back() method. So I need to cut off the tail of a file, represented
as std::basic_filebuf< char >, and keep an initial segment.

Is there a standard/protable way of doing this. Currently, I use a hack
invoking the POSIX ftruncate system call. I understand that this is not
portable.

I had a look at the draft copies of the standard freely available on the
internet, but the methods in basic_filebuf seem not to allow for this.
The only way that I see is to copy the initial part that I want to keep
to a new file and ditch the old one. But that seems to be inefficient.


Thanks for your consideration

Kai-Uwe Bux
 
J

JustSomeGuy

Hi,

I am writing a container template file_of< POD > that should resemble
std::vector< T > as closely as feasible given that the container actually
interfaces to an underlying file on disk. E.g., insertion can take
place *only* at the end. Nonetheless, I would like to implement the
pop_back() method. So I need to cut off the tail of a file, represented
as std::basic_filebuf< char >, and keep an initial segment.

Is there a standard/protable way of doing this. Currently, I use a hack
invoking the POSIX ftruncate system call. I understand that this is not
portable.

I had a look at the draft copies of the standard freely available on the
internet, but the methods in basic_filebuf seem not to allow for this.
The only way that I see is to copy the initial part that I want to keep
to a new file and ditch the old one. But that seems to be inefficient.


Thanks for your consideration

Kai-Uwe Bux

not sure if this helps but if you fseek to the position where you
wish to truncate to then write 0 bytes the file will be truncated.
 
Q

qWake

Hi,

I am writing a container template file_of< POD > that should resemble
std::vector< T > as closely as feasible given that the container actually
interfaces to an underlying file on disk. E.g., insertion can take
place *only* at the end. Nonetheless, I would like to implement the
pop_back() method. So I need to cut off the tail of a file, represented
as std::basic_filebuf< char >, and keep an initial segment.

Is there a standard/protable way of doing this. Currently, I use a hack
invoking the POSIX ftruncate system call. I understand that this is not
portable.

I had a look at the draft copies of the standard freely available on the
internet, but the methods in basic_filebuf seem not to allow for this.
The only way that I see is to copy the initial part that I want to keep
to a new file and ditch the old one. But that seems to be inefficient.


Thanks for your consideration

Kai-Uwe Bux

I have unsuccessfully looked for a standard way to do that in the past and
failed to find one. I believe the problem is that file systems vary and so
they are not defined in the C++ standard. This means that any solution is
operating system (or rather file system) dependent. I hope someone will
prove me wrong, but I don't think so. Sorry!
 
J

Jack Klein

not sure if this helps but if you fseek to the position where you
wish to truncate to then write 0 bytes the file will be truncated.

This is not a guarantee or requirement of either the C or C++ language
standards. It is just a non-portable hack that happens to work on
some implementations and platforms. There are others where it does
not work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top