Gossamer Forum
Home : General : Perl Programming :

Print contents of a file...

Quote Reply
Print contents of a file...
I would like to open a file and print its contents.

I am using this code but all that is printed in a number "1".

my $datafile = "data.db";
open(FILE, "$datafile") || die "I can't open $datafile\n";
@file = <FILE>;
close(FILE);
$data = @file;
print $data;



Any ideas?



Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
There are several ways of doing this;


$datafile = "data.db";
open(FILE, "$datafile") || die "I can't open $datafile\n";
@file = <FILE>;
print @file;

or

$datafile = "data.db";
open(FILE, "$datafile") || die "I can't open $datafile\n";
while (<FILE>) {
print $_;
}

or

$datafile = "data.db";
open(FILE, "$datafile") || die "I can't open $datafile\n";
@file = <FILE>;
print @file;
foreach $line (@file) {
print "$line";
}


Any of these should work..
I started learning perl three weeks ago. So I cant assure it`s correct but I think so.





Tom Erik Isaksen
Quote Reply
Re: Print contents of a file... In reply to
Thanks for the help...I actually tried a few different ways using code similar to the first example you provided but it keeps printing a number 1 or says it cant open the db file even though the file itself and the directory it is in, is chmodded to 777.

I'll try the second method...

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
Now it is saying that the data file is giving a bad header.

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
Ah....thankyou...I have this working now..

One more question...

All the printed text appears in one big block like...


llllllllllllllllllllllllllll
llllllllllllllllllllllllllll
llllllllllllllllllllllllllll

....but in the database file it looks like...


lllllllllllllllllllllllllll

lllllllllllllllllllllllllll

llllllllllllll

llllllllllllll

...how do I get it to print the text as it appears in the database?

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
hmm.. it should print it out just as it is in the database. I just tested all three codes with the same result.



Tom Erik Isaksen
Quote Reply
Re: Print contents of a file... In reply to
Do you mean when you tried it worked or are you having the same problem as me?

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
It works for me yes.




Tom Erik Isaksen
Quote Reply
Re: Print contents of a file... In reply to
Which code snippet were you using?

Im using the second example you gave...

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)
Quote Reply
Re: Print contents of a file... In reply to
Oh ....I fixed the problem.....

This is working for me now....

$file = "data.db";
open(FILE, "$file") || die "I can't open $file\n";
@file = <FILE>;
foreach $line (@file) {
print "$line<p>";
}

Paul Wilson. Shocked
(Dont blame me if I'm wrong!)