[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] Updating Files



On Thu, Jun 17, 1999 at 12:21:45PM -0700, Mark Yannuzzi wrote:
> I have a script that generates a data file that I wish to update.  The files
> structure is simply:
> 
> 1st Line: Date & Time Stamp.
> 2nd Line: Data Column Labels.
> nth Line: Columnar Data.
> 
> I need to:
> 
> 1) Update the first two lines of the file.
> 2) Append data to the file.

This is an FAQ.

perlfaq5:

     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?


> Depending on the structure of the new data that I wish to add to the
> existing data, it may be necessary for me to change (add to the end of the
> line) the label line to accommodate additional data columns.
> 
> The '>>' option for the OPEN command is clearly not what I need, since I
> need to read & rewrite the first two lines of the existing data file.
> 
> I tried '+>>' with OPEN, and used 'seek' and substitute operations, but this
> simply appended the lines, instead of replacing the first two.
> 
> How does one do this?  Is a temp file the only way, whereby:
> 1) Create temp file and write existing files data to it, and close both.
>    Or simply duplicate the existing file to create the temp file (I don't
>    know if there is a file command in MacPerl to do this, or not).
> 2) OPEN '>' the existing file, write the two new date/time & label lines.
> 3) Open and copy the temp files data, less the first two lines.
> 4) Append new data.
> 
> The problem I see here, if this is indeed the way to do it, is that the
> Creation date of my existing data file will change.  I do not want this to
> happen.
> 

A temp file is one way.  Another way is to open for reading and writing
with '+<'.  I believe that opening for append ('>>' or '+>>') seeks to the
end of the file before each write, which is why that did not work for you.

Ronald


==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org