?
Solved

reading and writing files

Posted on 2003-02-25
7
Medium Priority
?
185 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
[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
7 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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 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