How to truncate few bytes of file

--bcaec52be4ef2e2fac04a3b255d8
Content-Type: text/plain; charset=ISO-8859-1

Hi All,

I need to truncate last few bytes of file. these files are big in size.

One idea is to write needed bytes to another file and delete the original
file, but i am dealing with big files :(

dont want to use truncate, it just truncating the size, all data is gone

any pointers

Thanks
a b

--bcaec52be4ef2e2fac04a3b255d8--
chillidba [ Fr, 20 Mai 2011 12:10 ] [ ID #2059843 ]

Re: How to truncate few bytes of file

On Fri, May 20, 2011 at 03:40:35PM +0530, a b wrote:

> Hi All,
>
> I need to truncate last few bytes of file. these files are big in size.
>
> One idea is to write needed bytes to another file and delete the original
> file, but i am dealing with big files :(
>
> dont want to use truncate, it just truncating the size, all data is gone
>
> any pointers

truncate()'s second parameter is the length to which you want to
truncate. Did that not work for you? If not, I would suggest posting
some example code that fails, because calling truncate() seems to be the
correct approach here.

--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Paul Johnson [ Fr, 20 Mai 2011 14:37 ] [ ID #2059844 ]

Re: How to truncate few bytes of file

--0016e65a0f9820cc3804a3b4de57
Content-Type: text/plain; charset=ISO-8859-1

Hey Thanks all!

I got it ;)

open (FH, "+< $fname")|| die "\nFailed to open file $fname\n";
my $tmp=$fsize-$trunccount;
seek(FH,$tmp,0);
$addr = tell(FH) ;
truncate(FH, $addr)|| die "\nFailed to truncate $file: $!";
close(fd);
print "\nTruncate successful\n";



On Fri, May 20, 2011 at 6:07 PM, Paul Johnson <paul [at] pjcj.net> wrote:

> On Fri, May 20, 2011 at 03:40:35PM +0530, a b wrote:
>
> > Hi All,
> >
> > I need to truncate last few bytes of file. these files are big in size.
> >
> > One idea is to write needed bytes to another file and delete the original
> > file, but i am dealing with big files :(
> >
> > dont want to use truncate, it just truncating the size, all data is gone
> >
> > any pointers
>
> truncate()'s second parameter is the length to which you want to
> truncate. Did that not work for you? If not, I would suggest posting
> some example code that fails, because calling truncate() seems to be the
> correct approach here.
>
> --
> Paul Johnson - paul [at] pjcj.net
> http://www.pjcj.net
>

--0016e65a0f9820cc3804a3b4de57--
chillidba [ Fr, 20 Mai 2011 15:12 ] [ ID #2059846 ]

Re: How to truncate few bytes of file

>>>>> "ab" == a b <testabhi [at] gmail.com> writes:

ab> I need to truncate last few bytes of file. these files are big in size.

ab> One idea is to write needed bytes to another file and delete the original
ab> file, but i am dealing with big files :(

ab> dont want to use truncate, it just truncating the size, all data
ab> is gone

that doesn't make any sense. you want to truncate but not truncate??

do you want the size to remain the same but become null bytes? if so
that isn't truncating but overwriting. clarify your actual needs so we
can help here.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 20 Mai 2011 18:04 ] [ ID #2059849 ]

Re: How to truncate few bytes of file

>>>>> "ab" == a b <testabhi [at] gmail.com> writes:

ab> Hey Thanks all!
ab> I got it ;)

ab> open (FH, "+< $fname")|| die "\nFailed to open file $fname\n";

you don't need to open the file at all. truncate can take a filename.

ab> my $tmp=$fsize-$trunccount;

where do those get set? don't name vars $tmp as that tells the reader
nothing about the variable's use.

ab> seek(FH,$tmp,0);
ab> $addr = tell(FH) ;

you don't need the seek and tell calls. you have the size you want and
you can pass that directly to truncate.


ab> truncate(FH, $addr)|| die "\nFailed to truncate $file: $!";
ab> close(fd);
ab> print "\nTruncate successful\n";


this can all be reduced to this:

my $size = -s $file ;
truncate( $file, $size - $truncount ) ||
die "\nFailed to truncate $file: $!";

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Uri Guttman [ Fr, 20 Mai 2011 18:09 ] [ ID #2059850 ]

Re: How to truncate few bytes of file

--0016e64c1832491a6f04a3ed51c9
Content-Type: text/plain; charset=ISO-8859-1

Hey Thanks uri!!!

Actually i have a global variable which contains information of all files
and their size

you have given great directions

Thanks all of you!!

Regards
a b


On Fri, May 20, 2011 at 9:39 PM, Uri Guttman <uri [at] stemsystems.com> wrote:

> >>>>> "ab" == a b <testabhi [at] gmail.com> writes:
>
> ab> Hey Thanks all!
> ab> I got it ;)
>
> ab> open (FH, "+< $fname")|| die "\nFailed to open file $fname\n";
>
> you don't need to open the file at all. truncate can take a filename.

ab> my $tmp=$fsize-$trunccount;
>
> where do those get set? don't name vars $tmp as that tells the reader
> nothing about the variable's use.
>
> ab> seek(FH,$tmp,0);
> ab> $addr = tell(FH) ;
>
> you don't need the seek and tell calls. you have the size you want and
> you can pass that directly to truncate.
>
>
> ab> truncate(FH, $addr)|| die "\nFailed to truncate $file: $!";
> ab> close(fd);
> ab> print "\nTruncate successful\n";
>
>
> this can all be reduced to this:
>
> my $size = -s $file ;
> truncate( $file, $size - $truncount ) ||
> die "\nFailed to truncate $file: $!";
>
> uri
>
> --
> Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com--
> ----- Perl Code Review , Architecture, Development, Training, Support
> ------
> --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com---------
>

--0016e64c1832491a6f04a3ed51c9--
chillidba [ Mo, 23 Mai 2011 10:32 ] [ ID #2059943 ]
Perl » gmane.comp.lang.perl.beginners » How to truncate few bytes of file

Vorheriges Thema: Can't call method "findnodes" on unblessed reference; not using OO Perl
Nächstes Thema: it told me that Can't open perl script " hello.p1" No such file or directory