Forum Moderators: coopster & phranque

Message Too Old, No Replies

Reading and writing different flatfile databases

perl database flatfile csv

         

hwoods

8:10 am on Mar 15, 2003 (gmt 0)

10+ Year Member



I have a current project which requires the following :-

1. HTML form passes through parameters to CGI script
2. CGI opens file (new.txt)
3. reads first line of file
4. removes that line from the script
5. closes file
6. opens another file (old.txt)
7. append new record using record read from first file, plus some of the form items from the HTML
8. closes this second file
9. outputs to HTML details posted into second file

I'm across all of these, except for point 4, where I want to remove a line from the original file prior to closing it.

Any suggestions would be welcomed...

Brett_Tabke

10:09 am on Mar 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Welcome to the forums. Php or Perl?

Either way, it's a sequential file horse/cart syndrome. To delete the previous line of any file, you'll have to rewrite the file.

That can be done two ways:

copy the remaining lines to a new file, then delete the orginal, then rename the new one.

Or, read in the entire file into memory, and rewrite it with the updated data that leaves out the first line.

[edited by: Brett_Tabke at 10:57 am (utc) on Mar. 15, 2003]

hwoods

10:48 am on Mar 15, 2003 (gmt 0)

10+ Year Member



I'm actually using perl (unfortunately the host I use does not support PHP, SQL or anything apart from Perl).

I was thinking that I can easily grab the first line by using :-
$record = <filehandle>

Can I then use
@ecords = <filehandle>
to grab the whole file, then use a loop to write back from line two (which I think perl sees as record 1) onwards

Thanks for your help..

Brett_Tabke

10:59 am on Mar 15, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Yep. I like the read in a line, write it to file, then delete old file and rename new file method better. It makes the code more transportable because you never know how big the file is going to be. If it accidently grows to megs and megs, it will fail on out-of-memory errors when you try to read the whole file in.