Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problems reading from a textfile.

Problems reading from a textfile.

Problems reading from a textfile.

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!    

RE: Problems reading from a textfile.

if you include some of your code then maybe we can help you.

RE: Problems reading from a textfile.

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.

RE: Problems reading from a textfile.

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.

RE: Problems reading from a textfile.

I´ll try that,

Thank you! :)

RE: Problems reading from a textfile.

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 = "/temp/info.txt";
my $OUTFILE = "/temp/master.txt";

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

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

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

     if ( -r $f1 )
     print "File: $f1 exists and is readable\n";
     print "File: $f1 does not exist and/or is not readable\n";

  while (<$f1>)
    my $csv2 = Text::CSV_XS->new;
    print "$_\n";
    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 "\$date = $date\n\n";
        #print "\$time = $time\n\n";
        ($hr, $min, $sec) = split /:/, $time;
        ($mon, $day, $yr) = split /-/, $date;

        #print "\$hr = ", $hr, "\n";
        #print "\$min = ", $min, "\n";
        #print "\$sec = ", $sec, "\n\n";

        #print "\$mon = ", $mon, "\n";
        #print "\$day = ", $day, "\n";     
        #print "\$yr = ", $yr, "\n\n";
        if ( $mon == $ymon && $day == $yday && $yr == $yyr )

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

       bail("parse() failed: $!\n");


RE: Problems reading from a textfile.

Or, after you

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

#process $_ , which is the file
#and it will give it to you line by line...

} #end while.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close