473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem Reading From File

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
14 2554
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
3,509 Expert Mod 2GB
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
RonB
589 Expert Mod 512MB
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
Jeff,

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

Regards,
Sidra
Feb 8 '10 #5
numberwhun
3,509 Expert Mod 2GB
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
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
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
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
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
chaarmann
785 Expert 512MB
...
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
3,509 Expert Mod 2GB
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
Thank you so much for your help. I am now able to read files and working more on it.
Regards.
Feb 12 '10 #14
.................................................. .................................................. ..........................
Mar 21 '10 #15

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

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
1
by: Boris Wilhelms | last post by:
Hello all, at first, sorry for my bad English, I’ll give my best  We have a strange problem reading Text- and VarChar-Fields. Our configuration: -Windows 2003 Server -MySQL Server 3.23.36...
6
by: The_Kingpin | last post by:
Hi again guys, I've decided to cut my project in section and I found it way easier like this. I'm having a little problem reading struct in a file though. I think after this I'll be able to...
1
by: Brad | last post by:
I'm having a problem reading a resource stream using the following syntax: Dim resStream As System.IO.TextReader = New _ ...
0
by: Manfred Braun | last post by:
Hi All, I have a problem reading queue-messages async. My QueueReader has a Start() and a Stop() method and if my app starts, it calls Start(). The problem is, that there are possibly several...
4
by: andreas.fabri | last post by:
I have a problem reading integers separated by commas with VC8 This program: ___________________________ // read.C #include <iostream> int main()
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
2
by: samar | last post by:
I have a C++ programme in MPI-environment which reads its Input from a file previously made by another C++ programme , but now I face a problem , the new programme can't read that data from the...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.