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

In reply to Re^2: Using seek function to write out to top of file? by o2bwise
in thread Using seek function to write out to top of file? by o2bwise

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.