Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Discarding contexts of a text file starting from an offset

Reply
Thread Tools

Discarding contexts of a text file starting from an offset

 
 
parthaspanda22@gmail.com
Guest
Posts: n/a
 
      11-05-2007
How can I get to discard the contents of a text file from a specified
offset(
say, obtained from ftell)?

Sincerely.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-05-2007
On Monday 05 Nov 2007 12:54 pm http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed) wrote in
<(E-Mail Removed) .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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-05-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-05-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-06-2007
On Sun, 04 Nov 2007 23:24:16 -0800, (E-Mail Removed) 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
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-06-2007
Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing offset-aware and offset-naive datetimes? Roy Smith Python 4 01-27-2013 03:17 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Translated Offset to Source Offset Lance Riedel XML 2 10-15-2003 03:04 PM



Advertisments