http://qs321.pair.com?node_id=7145

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to write a string to the BEGINNING of a file?
I really want/need (mainly want) to be able to do this!

Any help is great!

Replies are listed 'Best First'.
Re: Writing to files
by plaid (Chaplain) on Apr 08, 2000 at 02:18 UTC
    Two things:

    1. It's good to understand a little bit of theory as to why you can't write before the beginning of a file. Files are stored on disk with a fixed beginning, whether that be at the beginning of a block or a pointer in a tree.. however the particular filesystem stores files. So, in order to prepend to a file, what actually has to be done is contents of the file have to physically be moved forward, so that the prepended string is located at the beginning of the file. Basically, the way to do this is to read the entire file, write out the new string, and then write out the original contents of the file, as said above.

    2. The title of the section referenced above, "How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?", doesn't make sense. Append means to add at the end, it should be prepend.

Re: Writing to files
by turnstep (Parson) on Apr 07, 2000 at 22:52 UTC
    The short answer is, no, not as easily as you might think. Unlike say, word processing, you can't just go to the start of the file, hit the 'insert' key, and add a string: file editing is 'overwrite' mode only. There are two basic ways to do it, and both require you to read in all the data from the file at some point.
    • Open a new temporary file, add your string, then open the old file. While reading from the old file, write everything to the new file. Close both when done, then turn the new file into the old file through renaming.

      This is the "general solution" mentioned in the above link. It has the advantage of not using much memory, as you only have to store one line at a time in memory, instead of the whole file. It has the disadvantages of using a temporary file, making the renaming system calls, not working well with file locking, and not being able to work with the data as easily, although you can always read in the whole file (@myfile = <ODLFILE>) at the cost of the memory advantage mentioned above.

    • A second way is to not use a temporary file, but just open the file for reading and writing, slurping in the whole file, make changes/add strings, rewind the file via seek, then write the changed data back into the file. One truncate and one close later, and you are done.

      This method has the disadvantages of reading the whole file into memory, and perhaps being harder for those not familiar with seek and truncate. The advantages are not using a temporary file, not having to rename, and a better ability to make non line-by-line changes. It's also the best way to do it if you are file locking.

    I prefer the second way myself, but whatever floats your boat - as long as it gets the job done!

Re: Writing to files
by btrott (Parson) on Apr 07, 2000 at 22:28 UTC
RE: Writing to files
by Anonymous Monk on Apr 08, 2000 at 07:03 UTC
    I've got a program online that does insertion into a file (http://www.pobox.com/~japhy/tmp/perl/add_lines). But the bigger question is, WHY DO YOU NEED TO DO THIS? Observe:
    open FILE, ">>data" or die "can't append to data: $!"; print FILE "NEW RECORD\n"; close FILE; # now, to store the file's contents in an array, MOST # RECENT DATA FIRST, use unshift instead of push! open FILE, "data" or die "you know the drill... $!"; unshift @contents, $_ while <FILE>; close FILE;
      Why do you need to do what? Your question is not clear. Yes, you can unshift into an array, but that has nothing to do with writing the file...
        Not to put words in his mouth, but the point I think japhy (right?) was trying to make was that, if the reason you're writing to the beginning of the file is that you think that there's where you should store the most "recent" information, then there's really no point.

        Just keep your file in chronological order (oldest at beginning, newest at end) and, when you're reading the file into memory, use unshift rather than push so that the newest data ends up at the beginning of the array.

        Which definitely makes sense, and makes the problem quite a lot less complicated, if that's the reason the op wanted to write to the beginning of the file.

Re: Writing to files
by jbert (Priest) on Apr 10, 2000 at 19:11 UTC
    All the above good advice applies, but one liners are cool:
    perl -i -p -e "BEGIN{print 'A new line!\n';}" your files here
      I had better luck with this: perl -i -p -e 'print "first line\n" if $. == 1;' data It depends on the particular magic of $., of course.
      Unfortunately, this doesn't work, because the BEGIN occurs before STDOUT is redirected to the output file (the file you're writing to). Plus, in your case, you used single quotes to surround your "\n", so it didn't get interpolated.

      Here's what I get:

      % perl -i -p -e "BEGIN{print 'A new line!\n';}" foo A new line!\n%
      I tried this myself, the other day, but ran into the same problem.