Solved

Basic text file appending not working!

Posted on 2003-12-08
3
218 Views
Last Modified: 2013-12-25
Hi,
i am trying to write to a simple text file on my windows server.

I can open the file and read the contents. However I cannot seem to be able to write to the file. I think the code is right. But it simply does not append anything to the text file.


print "Content-type: text/html\n\n";
$file = 'path\checkbook.txt';
open (CHECKBOOK, "+>>" . $file) or die "cannot open $file: $!";      
flock (CHECKBOOK, 2) or die "cannot flock $file: $!";
my @file_contents = <CHECKBOOK>;  
$file_contents[0] = "Browser was " . $ENV{'HTTP_USER_AGENT'} . "<BR>\n";
$file_contents[1] = "They came from " . $ENV{'HTTP_REFERER'} . "<BR>\n";
$file_contents[2] = "The remote address was " . $ENV{'REMOTE_ADDR'} . "<BR>\n";
$file_contents[3] = "The remote host was " . $ENV{'REMOTE_HOST'} . "<BR>\n";
print CHECKBOOK @file_contents;  # print the new info to the file  
close (CHECKBOOK);

Can anyone help?

Thanks,
Owen
0
Comment
Question by:owenthornton
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 9899141
To append to a file, do something like

my $file = '/path/checkbook.txt';
open CHECKBOOK, ">>$file" or die "Can not append to $file $!\n";
flock CHECKBOOK,2;

print CHECKBOOK <<EOF;
Browser was  $ENV{'HTTP_USER_AGENT'} <br>
They came from  $ENV{'HTTP_REFERER'} <br>
The remote address was  $ENV{'REMOTE_ADDR'} <br>
The remote host was  $ENV{'REMOTE_HOST'} <br>
EOF

close CHECKBOOK;

0
 

Author Comment

by:owenthornton
ID: 9902144
Hi Tintin,

I stuck the code in the cgi file. That is all code the cgi file has in it, since under windows, i don't think you have to "declare" the path to perl.

with that code I get a

'pathtoscript/flatfile.cgi' script produced no output

error message.

Any ideas?
0
 
LVL 48

Accepted Solution

by:
Tintin earned 50 total points
ID: 9926596
You get the message "produced no output" because that's exactly what the script does.

If you want some output, add something like

print "Information written to $file\n";
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question