Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems reading from a textfile.

Status
Not open for further replies.

Fumac

Programmer
Jan 2, 2001
16
0
0
SE
I have a program that are supposed to fetch information from a textfile and modifie it. But there is one problem, The text files that are targets for this function contains hard brakes (\n). When my program notice the brake it stops to read. All information untill the brake is returned but I want all of the information from the text file, not only the first line.

How do I do this?

Thank you for any respons!
 
I use the 'open' function as seen below.

open (TEST, "../docs/images/imagebank1/imageinfo.txt");
$info = <TEST>;
close (TEST);

Now, $info will contain the first row in that text file.


 
if you want the entire contents of the file use

@info = <TEST>;


this will set each item in the info array to a line of the file.

 
Problem parsing a CSV file

I have a function which parses a CSV file then extracts records from that file and writes those records to another file. I pass references to the file handles to this function( a reference to $INFILE and $OUTFILE). When the function attempts to parse the CSV file I get the following parse error message: parse failed: No such file or directory. I am able to parse 2 other CSV files but this one particular generates this error message. Does anyone have any ideas on what could cause this parse error? Thanks
in advance for any help offered.

Here's the code:

In the main section:
my $INFILE = &quot;/temp/info.txt&quot;;
my $OUTFILE = &quot;/temp/master.txt&quot;;

open(IFILE, &quot;$INFILE&quot;) || die &quot;Can\'t open input file\n&quot;;
open(OFILE, &quot;$OUTFILE&quot;) || die &quot;Can\'t open output file\n&quot;;

# pass file handle references to function
writeToMasterFile(\*IFILE, \*OFILE);


# function
sub writeToMasterFile
{
my ($f1, $f2) = @_;

if ( -r $f1 )
{
print &quot;File: $f1 exists and is readable\n&quot;;
}
else
{
print &quot;File: $f1 does not exist and/or is not readable\n&quot;;
}

while (<$f1>)
{
chomp;
my $csv2 = Text::CSV_XS->new;
print &quot;$_\n&quot;;
if ( $csv2->parse($_))
{
my @mail_entry = $csv2->fields;
my($accttype, $fname, $mname, $lname, $email, $pphone, $bphone, $address, $city, $state, $pcode, $province, $country, $comments, $refcocust, $refcoacct, $branch, $date, $time) = @mail_entry;
#print &quot;\$date = $date\n\n&quot;;
#print &quot;\$time = $time\n\n&quot;;
($hr, $min, $sec) = split /:/, $time;
($mon, $day, $yr) = split /-/, $date;

#print &quot;\$hr = &quot;, $hr, &quot;\n&quot;;
#print &quot;\$min = &quot;, $min, &quot;\n&quot;;
#print &quot;\$sec = &quot;, $sec, &quot;\n\n&quot;;

#print &quot;\$mon = &quot;, $mon, &quot;\n&quot;;
#print &quot;\$day = &quot;, $day, &quot;\n&quot;;
#print &quot;\$yr = &quot;, $yr, &quot;\n\n&quot;;

if ( $mon == $ymon && $day == $yday && $yr == $yyr )
{

if ( $hr >= 12 )
{
print $f2 $_, &quot;\n&quot;;
}
elsif ( $mon == $tmon && $day == $tday && $yr == $tyr )
{
if ( $hr < 12 )
{
print $f2 $_, &quot;\n&quot;;
}
}
}

}
else
{
bail(&quot;parse() failed: $!\n&quot;);
}
}

}


 
Or, after you

open(&quot;/path/to/file&quot;, TEST);
you can do

while(<TEST>){
#process $_ , which is the file
#and it will give it to you line by line...

} #end while.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top