Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Deleting first N lines from a text file |
| Date | 2011-11-15 21:48 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <j9v89c$urd$1@dont-email.me> (permalink) |
| References | <469add1a-0497-4746-9c52-6c775ecd53d9@u5g2000vbd.googlegroups.com> <j9sl02$j6f$1@dont-email.me> <41199f8e-82e7-4b90-8b8a-c79dd437b1ec@p1g2000yqh.googlegroups.com> |
On 11/15/2011 7:57 PM, Giuseppe wrote:
> On 15 Nov, 04:06, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>>> What do you think about those thoughts? Do you agree with me?
>>
>> No, not at all. One problem with your supposedly simpler
>> solution: How do you tell subsequent readers of the file that they
>> should stop before reaching the end? Observe that<stdio.h> offers
>> no way to shorten an existing file to any length other than zero.
>
> Ok, I implemented the "temporary file" solution and it works well.
> The
> only disadvantage is time: when the file is big (1000 lines of about
> 50 bytes
> each), the time to delete the first line could be very high.
Fifty K shouldn't take long. Even on a system from forty years
ago it didn't take long. Even on paper tape, for goodness' sake, it
took less than a minute!
For "really big" files (terabytes) copying most of the file from
one place to another could take an unacceptably long time. Also, the
need to find space for a second nearly complete copy could be
troublesome. In such cases you'd be justified in seeking fancier
solutions -- but I sincerely doubt that "slide all those terabytes
a couple hundred positions leftward" would produce a savings. More
likely it would produce a slowdown, plus the risks you've already
mentioned about data loss in the event of an error. No, the fancier
solution would probably involve some kind of an index external to the
file, describing which parts of the file were "live" and which "dead,"
and fancier routines to read just the live parts.
> Do you think the process could be reduced launching an external script
> (for
> example, 'head' based) with system()? If I redirect the output to the
> original
> filename I could avoid the time consuming process of copying the
> original
> to the temporary file.
First, just what do you imagine the "head" program does, hmmm?
However, on the systems I've encountered that provide a "head"
utility and support "redirection," your solution is likely to run
very quickly indeed. And save a lot of disk space, too! (Hint:
Try it yourself: `head <foo.txt >foo.txt', then `ls -l foo.txt',
and then you get to test your backups ...)
But all this is mostly beside the point. You are worried about
the time to copy 50K bytes: Have you *measured* the time? Have you
actually found it to be a problem for your application? Or are you
just imagining monsters under your bed? The fundamental theorem of
all optimization is There Are No Monsters Until You've Measured Them.
--
Eric Sosman
esosman@ieee-dot-org.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Deleting first N lines from a text file pozz <pozzugno@gmail.com> - 2011-11-14 16:02 -0800
Re: Deleting first N lines from a text file Roberto Waltman <usenet@rwaltman.com> - 2011-11-14 19:21 -0500
Re: Deleting first N lines from a text file Acid Washed China Blue Jeans <chine.bleu@yahoo.com> - 2011-11-14 16:31 -0800
Re: Deleting first N lines from a text file blp@cs.stanford.edu (Ben Pfaff) - 2011-11-14 16:36 -0800
Re: Deleting first N lines from a text file Roberto Waltman <usenet@rwaltman.com> - 2011-11-14 20:10 -0500
Re: Deleting first N lines from a text file Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-16 11:04 +0200
Re: Deleting first N lines from a text file gordonb.3hlvm@burditt.org (Gordon Burditt) - 2011-11-14 18:53 -0600
Re: Deleting first N lines from a text file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-14 22:06 -0500
Re: Deleting first N lines from a text file Giuseppe <giuseppe.modugno@gmail.com> - 2011-11-15 16:57 -0800
Re: Deleting first N lines from a text file Keith Thompson <kst-u@mib.org> - 2011-11-15 17:50 -0800
Re: Deleting first N lines from a text file pozz <pozzugno@gmail.com> - 2011-11-15 23:11 -0800
Re: Deleting first N lines from a text file jgharston <jgh@arcade.demon.co.uk> - 2011-11-16 04:11 -0800
Re: Deleting first N lines from a text file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-15 21:48 -0500
Re: Deleting first N lines from a text file pozz <pozzugno@gmail.com> - 2011-11-15 23:14 -0800
Re: Deleting first N lines from a text file "BartC" <bc@freeuk.com> - 2011-11-16 14:26 +0000
Re: Deleting first N lines from a text file Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-16 21:32 -0500
Re: Deleting first N lines from a text file Ian Collins <ian-news@hotmail.com> - 2011-11-17 15:43 +1300
Re: Deleting first N lines from a text file "BartC" <bc@freeuk.com> - 2011-11-17 10:35 +0000
Re: Deleting first N lines from a text file Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-16 07:05 +0000
Re: Deleting first N lines from a text file Robert Wessel <robertwessel2@yahoo.com> - 2011-11-16 22:46 -0600
Re: Deleting first N lines from a text file gordonb.pa5u0@burditt.org (Gordon Burditt) - 2011-11-16 16:28 -0600
Re: Deleting first N lines from a text file Kaz Kylheku <kaz@kylheku.com> - 2011-11-16 22:53 +0000
Re: Deleting first N lines from a text file gordonb.z5i2o@burditt.org (Gordon Burditt) - 2011-11-16 18:34 -0600
Re: Deleting first N lines from a text file jacob navia <jacob@spamsink.net> - 2011-11-15 12:48 +0100
Re: Deleting first N lines from a text file "-.-" <none@none.none> - 2011-11-16 14:01 +0100
Re: Deleting first N lines from a text file jacob navia <jacob@spamsink.net> - 2011-11-16 14:44 +0100
Re: Deleting first N lines from a text file tom st denis <tom@iahu.ca> - 2011-11-17 10:43 -0800
Re: Deleting first N lines from a text file jt@toerring.de (Jens Thoms Toerring) - 2011-11-18 23:12 +0000
Re: Deleting first N lines from a text file jgharston <jgh@arcade.demon.co.uk> - 2011-11-16 06:42 -0800
Re: Deleting first N lines from a text file jgharston <jgh@arcade.demon.co.uk> - 2011-11-16 07:43 -0800
Re: Deleting first N lines from a text file Keith Thompson <kst-u@mib.org> - 2011-11-16 11:13 -0800
Re: Deleting first N lines from a text file jgharston <jgh@arcade.demon.co.uk> - 2011-11-16 13:50 -0800
csiph-web