in reply to File Truncation Question

Wow, I was just in the process in posting my code, which happens to be

open(DATA, ">/host/n/b/p/p/o/r/nbpdexplorers.port5.com/cgi-bin/data2.t +xt") or die "Cannot open database file: $!"; flock(DATA, 2); seek(DATA, 1, 0); foreach my $item (@currentdata) { print DATA "$item\n"; } flock(DATA, 8); close(DATA);

and I realized the only thing which was different from the other examples you gave me. I had been using the seek(DATA, 1, 0); because another monk (who I don't remember, sorry) had advised to use when writing to a file and using flock. I tried it without that line and it worked fine. Thanks so much for your examples and assistance.

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: Re: File Truncation Question
by jonnyfolk (Vicar) on May 26, 2003 at 07:47 UTC
    Aha! That's the bit of code you should have included in the first place :)

    Reading up on seek, which I hadn't used before, what you seem to be doing is correctly opening your file, and then searching in the file for the point where you wish to begin.

    seek(DATA, 1, 0);
    is actually telling perl to offset the file by 1 byte and since it's a newly created file it simply creates the offset before it does anything else, hence the initial white space.

    Normally you would use seek for fixed length records where you can be sure that the beginning of a particular record can be found at an exact number of bytes into the file, so you were either told this for a specific purpose which you took to be standard procedure, or you were just told wrong! :)

    As I mentioned this is from my reading and interpretation and I rather imagine I will be corrected by other more experienced monks so please read for yourself and draw your own conclusions rather than rely on mine!

    I hope this has been helpful to you...