hi, im trying to read a file line by line, and write it out to another file, but alternating each write such that it writes the line to the start of the file one time and to the end of the file the next. the code i have (below) gives me an output file where it seems that each time i move the filepointer to the start of the file and write, i'm overwriting the last thing i wrote at the start of the file. i am trying to have it so that each time i seek to the beginning, it seeks to the beginning and writes what i want before whatever is there (while preserving the existing data instead of overwriting it). here is the code i have:
open (FIN, "<$ARGV[0]") || die "Input file: $!\n"; open (FOUT, ">$ARGV[0]-randomized") || die "Output file: $!\n"; $flag = 1; #set autoflush on output filehandle $oldfh = select(FOUT); $| = 1; select ($oldfh); while (<FIN>) { if ($flag) { seek (FOUT, 0, 0) || die "Seek to beginning: $!\n"; $flag = 0; } else { seek (FOUT, 0, 2) || die "Seek to end: $!\n"; $flag = 1; } print FOUT $_; } close (FOUT); close (FIN);
any suggestions on how to actually accomplish what i'm going for would be greatly appreciated. :-)

In reply to rearrange lines in file by ZxCv

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.