Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Basic text file appending not working!

Posted on 2003-12-08
3
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

610 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