in reply to Using seek function to write out to top of file?

No, you'll get "The\nin\nthe\nhat\n". A file is an ordered sequence of octets. There's no way for seek to magically shift the previous contents down when you start writing at the beginning. See perldoc -q "append to the beginning" for more details.

--
We're looking for people in ATL

Replies are listed 'Best First'.
Re^2: Using seek function to write out to top of file?
by o2bwise (Scribe) on May 27, 2005 at 21:43 UTC
    Hi,

    Oh, tlm, I did not try your method, but (time permitting), I will give it a shot. I tried the following 8 cases.

    File t.txt:
    cat
    in
    the
    hat

    Case 1 (write,no + sign, use seek):
    #!/usr/bin perl -w open (TEST,">t.txt); seek (TEST,0,0); print TEST "The\n"; close (TEST);


    Case 2 (write, + sign, use seek):
    #!/usr/bin perl -w open (TEST,"+>t.txt); seek (TEST,0,0); print TEST "The\n"; close (TEST);


    Case 3 (append, no plus sign, use seek):
    #!/usr/bin perl -w open (TEST,">>t.txt); seek (TEST,0,0); print TEST "The\n"; close (TEST);


    Case 4 (append, use + sign, use seek):
    #!/usr/bin perl -w open (TEST,"+>>t.txt); seek (TEST,0,0); print TEST "The\n"; close (TEST);


    Case 5 (write, no plus sign, use sysseek):
    #!/usr/bin perl -w open (TEST,">t.txt); sysseek (TEST,0,0); syswrite TEST, "The\n"; close (TEST);


    Case 6 (write, use + sign, use sysseek):
    #!/usr/bin perl -w open (TEST,"+>t.txt); sysseek (TEST,0,0); syswrite TEST, "The\n"; close (TEST);


    <Case 7 (append, no + sign, use sysseek):
    #!/usr/bin perl -w open (TEST,">>t.txt); sysseek (TEST,0,0); syswrite TEST, "The\n"; close (TEST);


    Case 8 (append, use + sign, use sysseek):
    #!/usr/bin perl -w open (TEST,"+>>t.txt); sysseek (TEST,0,0); syswrite TEST, "The\n"; close (TEST);


    Results:
    Cases 1,2,5,6
    t.txt
    The

    Cases 3,4,7,8:
    t.txt
    cat
    in
    the
    hat
    The

    Conclusion:
    The results are driven by one thing; whether opening the file to write (>) or to append (>>). Using + doesn't do anything and using sysseek gives the same result as seek. Whether going to the beginning of the file with seek or with sysseek (and using append), writing was performed AT THE END OF THE FILE.

    I'm kind of surprised. The responses I got had the expectation that writing would be done at the top of the file. I have never seen this to be the case.

    One quick question. Does opening to write (one > sign) ALWAYS cause the file to be emptied? This is what I have seen. I have never seen it have previous text.

    A reminder. This was for an on-line class, which requested this procedure. I find the class documentation to have had three mistakes (for which I am disappointed).

    1. It assumed that opening with write (not append) might leave previous text.

    2. It assumed you could write out at the top of a file (that has text in it from previously) using syseek at 0,0.

    3. It also used flock on explicit file name and not filehandle.

    Thanks, monks, for your assistance.

    o2

      You need to read perldoc -f open and perldoc perlopentut as it explains all of this. Opening with any form of > clobbers the output, and any form of >> opens in append mode. If you want to update in place you need to use +<.

      --
      We're looking for people in ATL

        fletch,

        I finally tried out +<. That worked. I mean, I wouldn't have done it that way, but it worked with what the class asked for, for a procedure (using seek (filehandle,0,0) and truncate (filehandle, tell(filehandle)).

        This was a good learning experience, albeit a very narrow one.

        Thanks,

        o2