Solved

Basic text file appending not working!

Posted on 2003-12-08
3
211 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now