CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Open file 2 times writing then append

 



ezcomp
Deleted

Apr 21, 2000, 9:52 PM

Post #1 of 3 (432 views)
Open file 2 times writing then append Can't Post

Hi all,
Quick question, I am trying to open a file 1 time to I am updating some statistics.
open (NEWREG, ">$basepath$regdir/$form{'ALIAS'}.dat") or die "can't append to file: $!";
print NEWREG "$np\n$nnn\n$pnn\n";
close NEWREG;


then next time i need to append information to the end of the file.

open(REGFILE, ">>$basepath$regdir/$form{'ALIAS'}.dat") or die "can't append to file: $!";
print REGFILE "$form{'ALIAS2'}\[\]$form{'ITEM'}\[\]$form{'RATES'}\n";
close REGFILE;


However when I use both I cannot get the append to work.

with both it will update the counts correctly..but just overwrites the line i am trying to add information after.

if i take out the writing (update the stats) it will append to the end of the lines.

How can i accomplish both?


ezcomp
Deleted

Apr 22, 2000, 10:52 PM

Post #2 of 3 (432 views)
Re: Open file 2 times writing then append [In reply to] Can't Post

Hi Cure,
Thank you for your reply. The first time the file is opened, it is to update the data
$np
$nnn
$pnn
represent a running total. each time a form is submitted those 3 are updated to give me a running total.

the second time it is opened it is suppose to be appended below the last line.

Whats driving me crazy is that if I rem out the updating of the running totals. It appends correctly, It seems like it doesn't want to close the file when updating it and then re-open it for appending.

I tried what you wrote above and it does the same thing. when i run the script it writes
the data file like this
data1
data2
data3
test[]ITEM[]RATES

then i change RATES to RATE in the script then it writes
data1
data2
data3
test[]ITEM[]RATE

Once again overwriting the line instead of adding it on the next line like this.

data1
datab
data3
test[]ITEM[]RATES
test[]ITEM[]RATE

I hope that explains it a little better than my first post it was late at night.


Cure
User

Apr 22, 2000, 11:06 PM

Post #3 of 3 (432 views)
Re: Open file 2 times writing then append [In reply to] Can't Post

Hi

You are NOT appending here, you are
opening a fresh empty file.

open (NEWREG, ">$basepath$regdir/$form{'ALIAS'}.dat") or die "can't
append to file: $!";

#################

Now you are appending the following to what you wrote to the new file
earlier.

open(REGFILE, ">>$basepath$regdir/$form{'ALIAS'}.dat") or die "can't
append to file: $!";
##########################3


The append works for me. The following test script:

#!/usr/bin/perl
$np = 'data1'; $nnn = 'data2'; $pnn = 'data3';
open (NEWREG, ">test.dat") or die "can't append to file: $!";
print NEWREG "$np\n$nnn\n$pnn\n";
close NEWREG;
open(REGFILE, ">>test.dat") or die "can't append to file: $!";
print REGFILE "test\[\]ITEM\[\]RATES\n";
close REGFILE;


Did you expect the first open to be an append? Then use >> instead of >.

Cure

[This message has been edited by Cure (edited 04-23-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives