?
Solved

reading and writing files

Posted on 2003-02-25
7
Medium Priority
?
187 Views
Last Modified: 2013-12-25
i have started a script that will have a form for a user to register: the contents of the form is then sent to a flat text file which uses | to seperate usename and password.
this is my code so far




#!c:\program files\active perl\bin\perl
use CGI qw(:cgi-lib);
&ReadParse(*input);
$uname= $input{'username'};
$pword=$input{'password'};
print "Content-type: text/html\n\n";
print "<HTML><BODY>";
print "Hi $uname, welcome! Thank you for registering with my site.\n";
open(DAT,">>names.cgi") || die("Cannot Open File");
print DAT "$uname\|$pword\n" ;
close(dat);
open(DAT,">>names.cgi") || die("Cannot Open File For The Second Time");
my @raw_data = <dat>;
close(dat);
foreach $uname (@raw_data)
{
chomp ($uname);
($usname,$paword)=split (/\|/,$uname);
print "username is $usname and password is $paword";
}

print "</BODY></HTML>";




when i try to run this code on the server it writes to the text file but it wont print the content of the text file like it is supposed to. i have worked out that this is because the line
 @raw_data = <dat>
is not working
can any1 tell me how to fix this plz!!!
0
Comment
Question by:gonewiththewind
5 Comments
 
LVL 7

Accepted Solution

by:
markhoy earned 100 total points
ID: 8016453
Try this and lock the file:
if (-e "names.cgi){
open(DAT,">>names.cgi") || die("Cannot Open File For The Second Time");
flock(DAT,2);
my @messages = <DAT>;
flock(DAT,8);
close(DAT);

$n=1;
foreach $msg (@messages){
print "<li>message # $n is $msg";
$n++
}


}else{
print "file doesn't exist";
}

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 8018853
1. you open a filehandle named DAT, then you use a filehandle named dat.
2. you open forwriting (">> ..."), then you try to read

fix these errors, and it should work
0
 

Assisted Solution

by:amitabhrai
amitabhrai earned 100 total points
ID: 8019046
Try this modified file. Also try to provide absolute path instead of just "names.cgi" for your username file.
=============
#!c:\program files\active perl\bin\perl
use CGI qw(:cgi-lib);
&ReadParse(*input);
$uname= $input{'username'};
$pword=$input{'password'};
print "Content-type: text/html\n\n";
print "<HTML><BODY>";
print "Hi $uname, welcome! Thank you for registering with my site.\n";

open(DAT,">>names.cgi") || die("Cannot Open File");
print DAT "$uname\|$pword\n" ;
close(DAT);

open(DAT,"<names.cgi") || die("Cannot Open File For The Second Time");
my @raw_data = <DAT>;
close(DAT);

foreach $uname (@raw_data)
{
chomp ($uname);
($usname,$paword)=split (/\|/,$uname);
print "username is $usname and password is $paword<BR>\n";
}

print "</BODY></HTML>";
===============================
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 100 total points
ID: 8021526
Here's a complete version, with locking, error checking and all the errors removed.

#!c:\program files\active perl\bin\perl
use strict;
use CGI::Carp qw(carpout fatalsToBrowser);
use CGI;
use Fcntl qw(:DEFAULT :flock);

my $q = new CGI;

my $uname = $q->param('username');
my $pword = $q->param('password');

print $q->header;

print "<html><body>";
print "Hi $uname, welcome! Thank you for registering with my site.\n";

open DAT,">>names.cgi" or die "Cannot open names.cgi because $!\n";
flock(DAT,LOCK_EX) or die "Could not obtain exclusive lock on file $!\n";
print DAT "$uname\|$pword\n";
close DAT;

open DAT,"names.cgi" or die "Cannot Open names.cgi for the second time because $!\n";

while (<DAT>) {
  chomp;
  my ($usname,$paword)=split (/\|/);
  print "username is $usname and password is $paword<br>\n";
}

close DAT;

print "</body></html>\n";
0
 
LVL 1

Expert Comment

by:lionheart620
ID: 8047993
or www.htmlite.com for all your CGI needs hahah. It has a great tutorial on Files, click CGI, then Files - 1,2,3 and 4 (at the bottom of the CGI menu).
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

621 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