473,402 Members | 2,053 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,402 software developers and data experts.

Discarding contexts of a text file starting from an offset

How can I get to discard the contents of a text file from a specified
offset(
say, obtained from ftell)?

Sincerely.

Nov 5 '07 #1
5 1969
On Monday 05 Nov 2007 12:54 pm pa************@gmail.com
pa************@gmail.com wrote in
<11**********************@z24g2000prh.googlegroups .com>:
How can I get to discard the contents of a text file from a specified
offset( say, obtained from ftell)?

Sincerely.
One method is to copy the relevant portion to another file, close the
first one and rename the second file to the name of the first one.
Another method could be to fill the unneeded portions with zero. One
more method is to read the relevant portion into memory, close the file
then open it again with the "w" mode and write your buffer out to it.

Nov 5 '07 #2
pa************@gmail.com said:
How can I get to discard the contents of a text file from a specified
offset(
say, obtained from ftell)?
If you just mean that you don't want to read any more from that file, then
don't read any more from that file.

If you want to delete the contents of the file from a specified point
onwards, there is no standard function to do that (although
implementation-specific functions are sometimes provided by library
implementors), but there is a standard *technique* to do it, which is very
simple:

* open the existing file for input
* open a new file for output
* if both those operations succeeded
* read from input all the data you want to keep,
and write it to the output (checking, of course,
that both reading and writing proceed correctly)
* close both files, checking that these operations
succeeded
* if all is still well
* delete the old file using remove()
* rename() the new file so that it takes the old name

That's it. Since this operation destroys data unrecoverably, make sure at
every step that all is well before proceeding to the next step. Remember
that, right up until the remove() call, it's still possible to change your
mind. After that, you have no (standard) way to recover the data.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nov 5 '07 #3
pa************@gmail.com wrote:
How can I get to discard the contents of a text file from a specified
offset( say, obtained from ftell)?
You can read the FAQ: <http://c-faq.com/osdep/ftruncate.html>.

Richard
Nov 5 '07 #4
On Sun, 04 Nov 2007 23:24:16 -0800, pa************@gmail.com wrote:
>How can I get to discard the contents of a text file from a specified
offset(
say, obtained from ftell)?
The portable way is to copy the file up to the desired point, delete
the original, and rename the copy.
Remove del for email
Nov 6 '07 #5
Richard Heathfield <r...@see.sig.invalidwrote:
parthaspand...@gmail.com said:
How can I get to discard the contents of a text file
from a specified offset(say, obtained from ftell)?

If you just mean that you don't want to read any more from
that file, then don't read any more from that file.

If you want to delete the contents of the file from a
specified point onwards, there is no standard function to
do that (although implementation-specific functions are
sometimes provided by library implementors), but there is
a standard *technique* to do it, which is very simple:
Simple to state, perhaps...
* open the existing file for input
* open a new file for output
What do you call this new file?

The obvious solution is to use a temp file, either via tmpnam
or tmpfile. The trouble is, many implementations will create
a temp file/name in a different directory from the file being
read.

This has consequences in a later step...
* if both those operations succeeded
* read from input all the data you want to keep,
and write it to the output (checking, of course,
that both reading and writing proceed correctly)
* close both files, checking that these operations
succeeded
* if all is still well
* delete the old file using remove()
* rename() the new file so that it takes the old name
Some versions of rename() do not allow you to rename across
disk drives, or other 'barriers'. If the temp file is not on
the same... um...'plane' as the original, the rename can fail.
That's it. Since this operation destroys data unrecoverably,
make sure at every step that all is well before proceeding
to the next step. Remember that, right up until the remove()
call, it's still possible to change your mind. After that,
you have no (standard) way to recover the data.
Other than offering to rewrite the temp file to a location of
the user's choice (and hoping it will succeed.)

There are other alternatives, though nothing perfect.

If the file in question is one that you've 'opened' for the
user, then many programs will immediately make a copy of it
and edit the copy, precisely so they can 'save' back over the
original file name and simply delete the copy.

Another option is to try the rename first, before copying.
If the rename fails, then you've saved time copying!

Perhaps the least robust in standard terms, but most robust
in practical terms, is to generate your own temp file name
simply by adding (say) '.tmp' to end of the name of the
original file.

--
Peter

Nov 6 '07 #6

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

Similar topics

11
by: Ren | last post by:
Suppose I have a file containing several lines similar to this: :10000000E7280530AC00A530AD00AD0B0528AC0BE2 The data I want to extract are 8 hexadecimal strings, the first of which is E728,...
7
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
3
by: spleen | last post by:
Hiya, Im working on a project to read in a file (its a bitmap, but thats irrelevant) and I want to read in all the data after an offset, ive tried using - fread(bitmap_buffer, offset, 1, fp);...
10
by: Kenneth Brody | last post by:
I recently ran into an "issue" related to text files and ftell/fseek, and I'd like to know if it's a bug, or simply an annoying, but still conforming, implementation. The platform is Windows,...
7
by: Ryan Liu | last post by:
Hi, I want to backup database to a text file. There are binary columns. I read byte from the column and use ASCII encoding convert it to string and write to a text file. But there are bytes...
9
by: Steven Bethard | last post by:
I have some text and a list of Element objects and their offsets, e.g.:: ... (etree.Element('a'), 0, 21), ... (etree.Element('b'), 11, 18), ... (etree.Element('c'), 18, 18), ... ] ...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.