Appending to a txt file?
Is there anyway to append information to the top of a page instead of on the bottom? My code below appends to the bottom. I know it has to do with the >>, just curious if theres a way to make it append on top?
my code snip:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
print <
Thank you, The Battle Has been added!!
EndOfHTML
open (TEMP_FILE, ">>battles.txt");
print TEMP_FILE <
$FORM{clanplayed}
$FORM{date}
$FORM{matchtype}
$FORM{score}
$FORM{map}
$FORM{outcome}
$FORM{comments}
EndOfHTML
close (TEMP_FILE);
Ted S posted this at 19:19 — 16th January 2001.
They have: 92 posts
Joined: May 1999
To add your code to the top of the file, you will need to do something like this:
perl... pase.... etc..
Please note, if you expect a large number of requests you may run into problems with file permisions. For example, if user X and user Y both submit the form at the same time you will either loose one entry, loose both entries or loose the whole file. The flocking code I provided will HELP prevent this but is not a 100% solution.
Ted S
Jane posted this at 19:46 — 16th January 2001.
They have: 4 posts
Joined: Jan 2001
So, with what you have posted my script should look like so..
#!/usr/bin/perl
print "Content-type:text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
print <
Thank you, The Battle Has been added!!
EndOfHTML
open (TEMP_FILE, "battles.txt"); #open without editing
my @battles = ; # store old stuff
close (TEMP_FILE); #close without editing
open (TEMP_FILE, "
$FORM{clanplayed}
$FORM{date}
$FORM{matchtype}
$FORM{score}
$FORM{map}
$FORM{outcome}
$FORM{comments}
~; #end new stuff print
print TEMP_FILE @battles; #print old stuff
flock TEMP_FILE, 8; #see end notes
close (TEMP_FILE); #close file and save
If thats right, I did it and the script seems to run, except its not printing anything to the battles.txt file?
My battles.txt is chmod 666 is that right?
[Edited by Jane on Jan. 16, 2001 at 02:51 PM]
Matt@Ikonboard posted this at 20:32 — 16th January 2001.
They have: 16 posts
Joined: Jan 2001
It won't, you need to open the filehandle for writing
#!/usr/bin/perl
print "Content-type:text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
open (TEMP_FILE, "battles.txt"); #open without editing
my @battles = <TEMP_FILE>; # store old stuff
close (TEMP_FILE); #close without editing
open (TEMP_FILE, ">battles.txt"); #----- Error was here ------#
flock TEMP_FILE, 2; #see end notes
print TEMP_FILE qq~ # new stuff to print at file top
<html>
<BODY BGCOLOR="#000000" text="#ffffff">
<table><tr><td>
<td width="11%" align="center"><font face="verdana" size="1" color="#ffcc33">$FORM{clanplayed}</font></td>
<td width="11%" align="center"><font face="verdana" size="1" color="#ffcc33">$FORM{date}</font></td>
<td width="11%" align="center"><font size="1" face="verdana, arial" color="#ffcc33">$FORM{matchtype}</font></td>
<td width="11%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">$FORM{score}</font></td>
<td width="18%" align="center"><font size="1" face="verdana, arial" color="#ffcc33">$FORM{map}</font></td>
<td width="10%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">$FORM{outcome}</font></td>
<td width="28%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">$FORM{comments}</font></td></table>
</BODY>
</HTML>
~; #end new stuff print
print TEMP_FILE @battles; #print old stuff
flock TEMP_FILE, 8; #see end notes
close (TEMP_FILE); #close file and save
You may want to avoid that problem with multiple submits by processing in place. eg
#!/usr/bin/perl -w
use CGI; #Save loads of work
use Fcntl qw(:DEFAULT :flock); #Import proper Flock functions
my $q->new CGI; #Define new CGI object
print $q->header(); #print content line
print <<EndOfHTML;
<html>
<body text="#FFCC33" bgcolor="#000000" link="#357B29" vlink="#357B29" alink="#357B29">
<center><font size="+1">Thank you, The Battle Has been added!!</font></center>
</body></html>
EndOfHTML
open ORIGINAL, "<battles.txt"; #Open FIle to read
flock ORIGINAL, LOCK_SH; #Apply shared lock
open TEMP, ">battles.tmp" or die $!; #Open Temp file to write
flock ORIGINAL, LOCK_EX; #Apply exclusive lock
select (TEMP); #Select Temp file for writing
print TEMP qq~
<html>
<BODY BGCOLOR="#000000" text="#ffffff">
<table><tr><td>
<td width="11%" align="center"><font face="verdana" size="1" color="#ffcc33">~ . $q->param(clanplayed) . qq~</font></td>
<td width="11%" align="center"><font face="verdana" size="1" color="#ffcc33">~ . $q->param(date) . qq~</font></td>
<td width="11%" align="center"><font size="1" face="verdana, arial" color="#ffcc33">~ . $q->param(matchtype) . qq~</font></td>
<td width="11%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">~ . $q->param(score) . qq~</font></td>
<td width="18%" align="center"><font size="1" face="verdana, arial" color="#ffcc33">~ . $q->param(map) . qq~</font></td>
<td width="10%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">~ . $q->param(outcome) . qq~</font></td>
<td width="28%" align="center"><font face="verdana, arial" color="#ffcc33" size="1">~ . $q->param(comments) . qq~</font></td></table>
</BODY>
</HTML>~;
while (<ORIGINAL>) { print TEMP $_ } # Print old stuff
close ORIGNAL;
close TEMP; #Closing always drops a lock, and is safer that flock 8 on unbuffered perl
rename ('battles.tmp', 'battles.txt'); #Rename files
select (STDOUT); # Reselect STDOUT for printing
exit (0); # If you want to exit
my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}
Jane posted this at 20:57 — 16th January 2001.
They have: 4 posts
Joined: Jan 2001
Thanks for all the input, got it workin!
Heres final Code.....
#!/usr/bin/perl
print "Content-type:text/html\n\n";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
print <
Thank you, The Battle Has been added!!
EndOfHTML
open (TEMP_FILE, "battles.txt"); #open without editing
my @battles = ; # store old stuff
close (TEMP_FILE); #close without editing
open (TEMP_FILE, ">battles.txt");
flock TEMP_FILE, 2; #see end notes
print TEMP_FILE <
$FORM{clanplayed}
$FORM{date}
$FORM{matchtype}
$FORM{score}
$FORM{map}
$FORM{outcome}
$FORM{comments}
EndOfHTML
print TEMP_FILE @battles; #print old stuff
flock TEMP_FILE, 8; #see end notes
close (TEMP_FILE); #close file and save
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.