View Full Version : Read File
cgibie
12-23-2005, 11:37 PM
Hi, I have a text file that looks like this;
Month: december
Yearh: 2005
Comment: Hello
My question is, I want to "read" that text file and print ONLY comment section NOT month and year. Are there ways to do that? Codes would be helpful.
my $outfile = "file.txt";
open(IN, "$outfile");
flock(IN, LOCK_SET);
seek(IN, 0, SEEK_SET);
#read the file
while (my $i = <N>) {
chomp($i);
print $i;
print "<br>";
}
Thanks in advance!
cyber11
12-23-2005, 11:58 PM
print $i if $i =~ m/^\s*comment:/i;
Theres also an error in the while loop. The file handle is <IN> not <N>
-Bill
cgibie
12-24-2005, 02:03 AM
I have another question,
I would like to click on a link and that link will take me to access that "file.txt" which is located in entry/2005/December/ folders. But my 2005 and my December folders are generated by the param on the form in "write" file (OUT). So when I code up the "read" (IN) file, I don't know how to access that file.txt from that folder.
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
require "path.cgi";
# print the header and the title of the page
print header;
print start_html("View The Blog");
# declare month and year params
my $year = param("year");
my $month = param("month");
#create the file name
my $time = localtime;
# declare the txt files from directories
my $file .= $entrypath . "/" . $year . "/" . $month . "/" . $month . "-" . $year . ".txt";
#my $file = "test.txt";
# open the text files
open(IN, "$file") or &dienice ("Could not open the $file: $!");
flock(IN, LOCK_SET);
seek(IN, 0, SEEK_SET);
cyber11
12-24-2005, 01:15 PM
I'm not clear as to what you don't understand.
You have a while loop that will read each line in the first post you made.
Although in this case print each line.
Is that not what you want?
Its also a good idea to clsoe your file when done.
After the while loop put:
close(IN);
-Bill
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.