473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to truncate a file

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
Jul 22 '05 #1
4 9090

<jk********@gmx.net> wrote in message
news:d6**************************@posting.google.c om...
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.
Jul 22 '05 #2
<jk********@gmx.net> wrote in message
news:d6**************************@posting.google.c om...
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!
Jul 22 '05 #3
"qWake" <ma**@qWake.com> wrote in message
news:10*************@corp.supernews.com...
<jk********@gmx.net> wrote in message
news:d6**************************@posting.google.c om... I have unsuccessfully looked for a standard way to do that in the past and
failed to find one.


Ok have a giggle, I did: I have unsuccessfully [...] failed to find one.
Jul 22 '05 #4
On Fri, 04 Jun 2004 04:42:33 GMT, "JustSomeGuy" <no**@nottelling.com>
wrote in comp.lang.c++:

<jk********@gmx.net> wrote in message
news:d6**************************@posting.google.c om...
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.


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yannick Turgeon | last post by:
Hello all, I just started to use perl and I'm having a Regexp question: Say we've got a file with this in it: ---- Beginning of file after this line -------- My list A: cat B: dog C: horse
1
by: Natevw | last post by:
I am using <fstream> to write files in binary mode. Is there any way I can force the file to end at a certain offset, without rewriting the whole file? I could open the file with ios::trunc, but...
1
by: New MSSQL DBA | last post by:
I have recently been assigned to take over several MSSQL environments and found some of the existing practice confusing. As most of my previous experiences are on Oracle and Unix platform so would...
4
by: Amit Kulkarni | last post by:
Hi, I have small problem. I want to truncate a line in a text file using C file handling functions and write new line in place of it. How do I do it? e.g. "example.txt" Line 1: This is a...
1
by: venkat | last post by:
Hi, Can any one tell me the best way to truncate a file if it exceeds a specified file size? I need to truncate the start of the file by the appropriate amount (The data to insert). Is there any...
5
by: ronin 47th | last post by:
Hi group, In one of the books 'Gurus Guide to Transact SQL' i found this info: ------------------------------------------------------------ TRUNCATE TABLE empties a table without logging row...
5
by: Neo | last post by:
I want to remove 10 bytes from end of the file, how I can perform this task using C filing functions? (Using vs2k5) I am not want to make new, write contents, remove input file and rename output...
1
by: hedgracer | last post by:
I would like to allow a particular user to truncate a log file in a stored procedure that the user runs every day. At this moment the only personnel that can truncate the log file are personnel...
2
by: s0suk3 | last post by:
file.truncate(X) will truncate the file to at most X bytes (i.e. leave the first X bytes of the file and throw away the rest). Is there a way to throw away, say, the first X bytes of the file, and...
5
by: Timothy Madden | last post by:
Hello I see there is now why to truncate a file (in C or C++) and that I have to use platform-specific functions for truncating files. Anyone knows why ? I mean C/C++ evolved over many years...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.