By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

Problem Reading From File

P: 17
hello...
i have recently started learning perl.....and having issues when i try to read input from another file. the details of a script that i tried to run are as follows:

i made a text file by the name “text.txt” and the contents were
Sidra|38|BE
Hira|48|BE
Shagufta|50|BE

Then i wrote the following script
Expand|Select|Wrap|Line Numbers
  1. open(DAT, "text.txt"); 
  2. $data_file=" text.txt ";
  3. open(DAT, $data_file); 
  4. @raw_data=<DAT>;
  5. close(DAT);
  6. foreach $student (@raw_data)
  7. {
  8.  chomp($student);
  9.  ($name,$roll_no,$class)=split(/\|/,$student);
  10.  print "The student  $name bearing roll number $roll_no is in class $class";
  11.  print "<br>\n";
  12. }
  13.  
the script produces no output and displays a message saying
“readline () closed filehandle at <filename> line <line number>”
I tried the same with another file by the name “text.dat” holding the same data but it did not work either. Please help me out resolving this issue.
Thankyou...
Feb 5 '10 #1
Share this Question
Share on Google+
14 Replies


P: 17
i have recently started learning perl. I am having a problem while reading input from a file. Kindly help me out to resolve this issue. The details of the script are as follows:

i made a text file by the name “text.txt” and the contents were

Sidra|38|BE
Hira|48|BE
Shagufta|50|BE

Then i wrote the following script
Expand|Select|Wrap|Line Numbers
  1. open(DAT, "text.txt"); 
  2. $data_file=" text.txt ";
  3. open(DAT, $data_file); 
  4. @raw_data=<DAT>;
  5. close(DAT);
  6. foreach $student (@raw_data)
  7. {
  8.  chomp($student);
  9.  ($name,$roll_no,$class)=split(/\|/,$student);
  10.  print "The student  $name bearing roll number $roll_no is in class $class";
  11.  print "<br>\n";
  12. }
  13.  
The script produces no output and displays a message saying
“readline () closed filehandle at <filename> line <line number>”
I tried the same with another file by the name “text.dat” holding the same data but it did not work either. Please help me out.
Thank you...
Feb 5 '10 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Sidra,

There were a couple things wrong with your post, but one has earned you a one time warning. That issue is that you hijacked a thread on this forum. If you have a question, you absolutely do not ask that question as part of someone elses thread. That is called hijacking and is against the rules and guidelines of this forum. This is your one and only warning against this and next time will result in a temporary ban.

The only other issue I had with your post was your lack of code tags use around the code you put in your post. Please use them as they are required.

Regards,

Jeff
Feb 5 '10 #3

Expert Mod 100+
P: 589
Please use the code tags when posting code.

You should ALWAYS check the return code of an open call to verify that is was successful and take action if it failed. It is better to use the 3 arg form of open and a lexical var for the filehandle instead of a bareword.

Every Perl script should include these 2 pragmas.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
Those pragmas will point out lots of simple mistakes, such as typos on vars, which could be difficult to track down otherwise. The strict pragma forces you to declare your vars, which is done with the 'my' keyword or in some rare cases with the 'our' keyword.

Why are you trying to open the file twice?

99% of the time you should loop over the data file line-by-line (or record-by-record instead of stuffing it into an array and then loop over that array.

This is clearly a homework assignment so I won't provide a complete and corrected script, but I will give you a few lines, which will actually give you 90% of the solution. I'm leaving in 1 of your mistakes, but the added error handling will point you toward the answer.
Expand|Select|Wrap|Line Numbers
  1. my $data_file = " text.txt ";
  2. open my $DAT, '<', $data_file or die "can't open <$data_file> $!";
  3.  
  4. while (my $student = <$DAT>) {
Feb 5 '10 #4

P: 17
Jeff,

I am grateful to you for pointing out my mistake. Please accept my apologies for it.

Regards,
Sidra
Feb 8 '10 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
What you are doing is a bit cludgy at best and certainly needs a bit of changes.

First, when opening a file, you want to not only specify whether you are reading from or writing to it, but also want to know if something happened while opening it.

So, you could re-write your file opening statement like this:

Expand|Select|Wrap|Line Numbers
  1. open(DAT, "<text.txt") or die "Could not open file: $!";
  2.  
The $! prints out the error saying why it couldn't open the file.

I recommend you read the open help page on perldoc.

Also, one other thing to note, you have top open() statement, both using the same FILEHANDLE. This is a no-no. Always use a different file handle for different files.

Regards,

Jeff
Feb 8 '10 #6

P: 17
I tried the following code:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. $data_file=" text.txt "; 
  3. open(DAT, $data_file) or die"terminate the file: $!"; 
  4. @raw_data=<DAT>; 
  5. close(DAT); 
  6. foreach $student (@raw_data) 
  7.  chomp($student); 
  8.  ($name,$roll_no,$class)=split(/\|/,$student); 
  9.  print "The student  $name bearing roll number $roll_no is in class $class"; 
  10.  print "\n";
  11.  
Now it is giving this error message:

Name "main::data_file" used only once: possible typo at ./file1.pl line 2
terminate the file: No such file or directory at ./file1.pl line 3
Feb 9 '10 #7

P: 17
this isn't helping either.i get the error " no such file or directory at file1.pl line3...but i do have the file text.txt written in the same directory and i have created it musing mkdir.. is there another command i should use to create "files"...

please direct me how to open this text.txt file .. i will make the changes to the script as you suggested myself..
thanks
Feb 9 '10 #8

Expert Mod 100+
P: 589
Take a closer look at the error message.

Putting the filename inside < > should have clued you in to the fact that ' text.txt' ( note the leading space ) is not the same filename as 'text.txt'.
Feb 9 '10 #9

P: 17
This might be a typing mistake here.
in my script, i used it as "text.txt"....
i executed the following script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w 
  2. $data_file="text.txt ";  
  3. open(DAT, $data_file) or die"terminate the file: $!";  
  4. @raw_data=<DAT>;  
  5. close(DAT);  
  6. foreach $student (@raw_data)  
  7. {  
  8.  chomp($student);  
  9.  ($name,$roll_no,$class)=split(/\|/,$student);  
  10.  print "The student  $name bearing roll number $roll_no is in class $class";  
  11.  print "\n"; 
  12.  
This code is giving the error message
Please guide me how to open a file.

Regards.
Feb 10 '10 #10

Expert Mod 100+
P: 589
in my script, i used it as "text.txt"
No you didn't.

The script now has a trailing space in the filename but the filename you just stated doesn't.
Feb 10 '10 #11

Expert 100+
P: 785
...
and at line 3 you forgot to put a single space after "die", before the double quotation mark.

Spaces are important! You should always check them.

Please use always code-tags around your code!
Feb 10 '10 #12

numberwhun
Expert Mod 2.5K+
P: 3,503
Sidra Nisar,

This is the fourth time in this thread that I have had to add code tags around the code that you have posted in this forum. Code tags are required around code you put in your posts, just as they are on a lot of sites. If you do not put them in, then we have to clean up behind you.

You need to put them in your posts around your code. If you are unaware of how to use code tags, then please see the help section that refers to them.

Regards,

Jeff
Feb 10 '10 #13

P: 17
Thank you so much for your help. I am now able to read files and working more on it.
Regards.
Feb 12 '10 #14

P: 17
.................................................. .................................................. ..........................
Mar 21 '10 #15

Post your reply

Sign in to post your reply or Sign up for a free account.