Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to 2 seperate text files

Status
Not open for further replies.

brad1978

Programmer
Feb 13, 2009
30
CA
Hey Guys,
Is it possible to write to 2 seperate text files from the same CGI script? I'm trying and cannot get the second "write" to work. Any suggestions?

my $temp = param('temp');
my $post = param('post');

if($temp eq "")
{
print "Temp has no value";
}
else
{
#print temp results first
open(OUT, ">../retirement_notes/denomyTemp.txt") or die "Cannot write to file.";
print OUT "$temp\n";
close(OUT);
print "Temp posts have been saved successfully<br /><br />";
}
if($post eq "")
{
print "post has no value";
}
else
{
#now print post results
print $post."<br /><br />";
open(spitOut, ">>../retirement_notes/denomyPosts.txt") or die "Cannot write to file.";
print spitOut "$post\n";
close(spitOut);

print "Approved posts have been saved successfully<br /><br />";
}
 
add $! to the error handling of your open functions:

Code:
 open(spitOut, ">>../retirement_notes/denomyPosts.txt") or die "Cannot write to file: [red]$![/red]";

That will return the reason the file can't be opened. Make sure to print an http header first before printing anything to the browser from a CGI script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Okay , What am I missing here?

#! /usr/bin/perl
system('cls');
my $temp = param('temp');
my $post = param('post');
my $ofhTemp = 'Temp.txt';
my $ofhPost = 'Post.txt';

if( $temp ) {
open(OUT,">$ofhTemp") or die "$!";
print OUT "$temp\n";
close(OUT);
print "Temp posts have been saved successfully<br /><br />\n";
}else{
print "Temp has no value\n";
}

if( $post ) {
print "$post<br /><br />\n";
open(OUT,">$ofhPost") or die "$!";
print OUT "$post\n";
close(OUT);
print "Approved posts have been saved successfully<br /><br />\n";
}else{
print "post has no value\n";
}
sub param {
return('OUTPUT FROM PARM');
}
----------------------------------------------------------------------
STDOUT:
Temp posts have been saved successfully<br /><br />
OUTPUT FROM PARM<br /><br />
Approved posts have been saved successfully<br /><br />

Temp.txt:
OUTPUT FROM PARM

Post.txt:
OUTPUT FROM PARM

 
when I saw param() I assumed you were running a CGI script and using the param() function of the CGI module.

Your code is working, whats the problem?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Forget my last post. I got ejaggers code confused with the OPs original post.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
ejaggers,
As you can probably tell, I've only been working with CGI for about 2 weeks. What is the line "system('cls');" doing in your code?
 
There's still an error... The first write to the temp file works fine, in the writing to the post file, my first "HERE" statement fires but my script seems to stop there. What am I doing wrong here?


!/usr/bin/perl -wT
#print "Content-type: text/html\n\n";
print header(-expires=>'-1d');
use CGI qw/:standard/;

my $temp = param('temp');
my $post = param('post');
my $tdir = "../retirement_notes/denomyTemp.txt";
my $pdir = "../retirement_notes/denomyPosts.txt";

if( $temp )
{
#print temp results first
open(OUT, ">$tdir") or die "$!";
print OUT "$temp\n";
close(OUT);
print "Temp posts have been saved successfully<br /><br />";
}
else
{
open(OUT, ">$tdir") or die "$!";
print OUT "$temp\n";
close(OUT);
print "Temp posts have been saved successfully<br /><br />";
}

if( $post )
{
#now print post results
print "HERE<br /><br />";
open(OUT,">>$pdir") or die "$!";
print OUT "$post\n";
close(OUT);
print "HERE";
print "Approved posts have been saved successfully<br /><br />";
}
else
{
print "post has no value";
exit;
}
 
Sorry, KevinADC,
I am using CGI not PERL. I have a form which contains 2 text areas, I am moving content from one to the other and them writing them back to text files. The temp file gets overwritten completely with NO values and the post file gets appended with the value of my post variable.
 
OMG...
Just figured it out. My post file didn't have 777 permissions. ARGH. So dumb. Thanks for the help everyone!
 
brad1978 (Programmer)

system('cls'); #clears the screen in windows
system('clear'); #clears the screen in unix

system() executes os commands.
 
I am using CGI not PERL

Actually you are using both. CGI is not a programming language, its a interface protocol (Common Gateway Interface), much like HTTP amd FTP and etc. Perl scripts were called CGI scripts long ago and the name has stuck all these years later. CGI facilitates the communication between the forms that send data and the server side script that accepts the data for processing.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top