PDA

View Full Version : text file format


cgibie
11-13-2005, 06:21 AM
Here is my code and ran well:

#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);

print header;
print start_html("Shout Box");

my $outfile="box.txt";

open(IN, "$outfile");
flock(IN, LOCK_SH);
seek(IN, 0, SEEK_SET);

#now read the file

while (my $i = <IN>) {
chomp($i);
print $i;
print "<br>";
}
close(IN);

print end_html;

My format in that txt file is, for example

patheydavehello

After the execution of the above code, it gives me:

pat
hey
dave
hello

I want to put a <hr> code between that, how can I achieve by modifying code or what? The target is:

pat
hey
---------
dave
hello
--------

OR another way that I want is:
pay says hey
----------
dave says hello

Either way, please help.

FishMonger
11-13-2005, 03:54 PM
My format in that txt file is, for example

patheydavehello

After the execution of the above code, it gives me:

pat
hey
dave
helloThe code you posted won't give you those results, so either the format of the text file is different or you're not showing us all of the code. In either case we're not going to be able to help unless we have the proper detatils.

There are 2 things in your code that you should look at.

1. When ever you open a file handle, always check the the return status.
open(IN, "$outfile") || die "can't open $outfile $!";

2. Your CGI::Carp use statment includes warningsToBrowser which importes the function, but you didn't execute it.
print header;
warningsToBrowser(1);
print start_html("Shout Box");

cgibie
11-15-2005, 02:25 AM
I have another cgi file that is mainly used for writing the input from the form to the text file. After that code is executed, in the text file would have whatever the user fills in just like what I described above. Here is what the code looks like:

#!/usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);

my $outfile = "box.txt";

print header;
print start_html("Thank You");
print h2("Thank You");

my %form;
foreach my $i (param()) {
$form{$i} = param($i);

open(OUT, ">>$outfile") or &dienice("Could not open $outfile: $!");
flock(OUT, LOCK_EX);
seek(OUT, 0, SEEK_END);
print OUT "$i: $form{$i}\n";
close(OUT);
}

sub dienice {
my($errmsg) = @_;
print "<p>$errmsg</p>\n";
exit;
}

print end_html;

FishMonger
11-15-2005, 03:20 AM
It's still unclear as to the exact format of your text file. This last bit of code shows a different format than the first.

From what I see, you've posted 3 different formats:

format 1:
field_name: field_value

format 2:
field_value
field_value
field_value

format 3:
field_valuefield_valuefield_value

---------

In this last version, you've opened and locked the file for each param. It would be better to open/lock the file once.

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw(:standard :cgi-lib);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);

my $outfile = "box.txt";
my %form = Vars;

print header;
warningsToBrowser(1);
print start_html("Thank You");
print h2("Thank You");

open(OUT, ">>$outfile") or dienice("Could not open $outfile: $!");
flock(OUT, LOCK_EX);
seek(OUT, 0, SEEK_END);

foreach my $i (keys %form) {
print OUT "$i: $form{$i}\n";
}

flock(OUT, LOCK_UN);
close OUT;
print end_html;

sub dienice {
my($errmsg) = @_;
print "<p>$errmsg</p>\n";
exit;
}

cgibie
11-15-2005, 05:37 PM
Okay the code works fine. I wonder though what made the difference between looping each param and just once?

The code that you wrote looks good in my text file format, however, back to my first code regarding reading that text file, it still does not give me format:

field1
value1
--------
field2
value2

when they are printed out on the web page.

Does this make sense?

FishMonger
11-15-2005, 07:58 PM
Given the last format you specified i.e.,
key: value

#!/usr/bin/perl

use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);

my $file = "box.txt";
my %file;

print header;
warningsToBrowser(1);
print start_html("Shout Box");

open(IN, "$file") or dienice("Could not open $file: $!");
flock(IN, LOCK_SH);
seek(IN, 0, SEEK_SET);

#now read the file

while (my $i = <IN>) {
chomp $i;
if(my ($key, $value) = split(/: /, $i)) {
print "$key<br>$value<br>\n<hr>";
# or
print "$key says $value<br>\n<hr>";
}
}
flock(IN, LOCK_UN);
close(IN);
print end_html;

sub dienice {
my $errmsg = shift;
print "<p>$errmsg</p>\n";
exit;
}