473,416 Members | 1,838 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,416 software developers and data experts.

Trying to access data and print it to the screen using Perl Builders I/O Window

37
Trying to access data and print it to the screen using Perl Builders I/O Window
--------------------------------------------------------------------------------

Hi
I am using a program called Perl Builder which is
an Integrated Development Environment for Perl scripting.
I am trying to access the data in a txt file and then print
the results to the screen.
The procedure for doing this is write a script then click on the
Run button, which takes you to an interactive I/O window.
You then type information into a I/O box Click Enter and the
content should display.

Using the following script:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. print "Enter file name: "; # Get the file name.
  4. $name = <STDIN>;
  5. chop($name);
  6.  
  7. open(INPUT, $name) || # Try to open the file.
  8. die "Cannot open $name";
  9.  
  10. print "Contents of $name:\n";
  11.  
  12. while ($line = <INPUT>) # Print the file on the screen.
  13. {
  14. print $line;
  15. }
  16.  
  17. close(INPUT); # Close the file.
  18.  
  19. exit 0;

I get the following error:

Expand|Select|Wrap|Line Numbers
  1. Enter file name: Cannot open .mydatabase.txt at test line 18, <STDIN> line 1.

I thought maybe it had something to do with the file not being in the Working directory.
But when I try to access the same file using a file handle and the Cwd module, the content displays as it should.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. ##Getting current directory information
  3. use Cwd ;
  4. $directory = cwd ;
  5.  
  6. ##Setting variable to database name
  7. $file = "$directory/mydatabase.txt" ;
  8.  
  9. ##Opening up the file for reading
  10. open ( CTRL, $file ) ;
  11.  
  12. ##reading the contents of the file and
  13. ##putting it to the array @fruit
  14. @fruit = <CTRL> ;
  15.  
  16. ##closing the file
  17. close ( CTRL ) ;
  18.  
  19. ##running through each item in @fruit
  20. foreach ( @fruit ) 
  21.  
  22. ##taking of the return of each item
  23. chomp ( $_ ) ;
  24.  
  25. ##printing each item on a new line
  26. print "$_\n" ;
  27. exit;
So why will it not recognize the mydatabase.txt file using the I/O box

Any help would be welcome.

Regards Max
Mar 10 '08 #1
3 2301
numberwhun
3,509 Expert Mod 2GB
To begin with, I would neaten up and change your script to be:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. print("Enter file name: ");      # Get the file name.
  7. my $name = <STDIN>;
  8. chomp($name);  # Chomp removes the newline, not chop()
  9.  
  10. open(INPUT, $name) or die "Cannot open $name:  $!";   # Try to open the file.
  11.  
  12. print "Contents of $name:\n";
  13.  
  14. while (<INPUT>) # Print the file on the screen.
  15. {
  16.      print("$_");
  17. }
  18.  
  19. close(INPUT);      # Close the file.
  20.  
  21.  
As for why it cannot find the file, I am not sure. Is the file there? Do you have permissions to at least read it? The "$!" in the die statement of the open will give you the error that is produced when the script tries to open the file.

Regards,

Jeff
Mar 10 '08 #2
KevinADC
4,059 Expert 2GB
Use the full path to the file
Mar 10 '08 #3
Max58kl
37
To begin with, I would neaten up and change your script to be:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/contrib/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. print("Enter file name: ");      # Get the file name.
  7. my $name = <STDIN>;
  8. chomp($name);  # Chomp removes the newline, not chop()
  9.  
  10. open(INPUT, $name) or die "Cannot open $name:  $!";   # Try to open the file.
  11.  
  12. print "Contents of $name:\n";
  13.  
  14. while (<INPUT>) # Print the file on the screen.
  15. {
  16.      print("$_");
  17. }
  18.  
  19. close(INPUT);      # Close the file.
  20.  
  21.  
As for why it cannot find the file, I am not sure. Is the file there? Do you have permissions to at least read it? The "$!" in the die statement of the open will give you the error that is produced when the script tries to open the file.

Regards,

Jeff

Thanks for the advice!
The problem was the word, contrib in the path to the perl exe

#!/usr/contrib/bin/perl -w

I tried

#!/usr/bin/perl -w

and the file information I needed printed to the screen.

Regards Max
Mar 11 '08 #4

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

Similar topics

2
by: Carl | last post by:
Hi, I tried to print a document with the window.print method, but with netscape 7, i always get the error:"the page was replaced while you was trying to print. Try again". I see "mydoc.htm" and...
2
by: moller | last post by:
Im looking in to the possibility of moving from mySQL to an access database. My reasons are: (1) Database is single user. (2) Database local on users PC. (3) Database has only 8 tables where 4...
0
by: Malcolm Cook | last post by:
I've discovered: Using "File > Print Preview" in a form's datasheet view with a server Filter crashes access after previewing... ....that is, unless ServerFilterByForm has been turned off...
2
by: Maryam | last post by:
Is a split window possible for an MS DOS program and MS Access program?? If so how??? When you run a DOS programn it covers the whole of the PC screen and if you have other applications you...
3
by: Mike S | last post by:
Anyone know how I can access a MS Access database from perl? Any help appreciated.
8
by: stemo76 | last post by:
I have created a report with subform/subreports. I can see all the data on the screen but when I try to print it to a printer or adobe distiller I get 'The Microsoft Jet Database engine could not...
2
by: bhav | last post by:
Hi Guys, I have a Web Forms' 'Table' control in my default.aspx page, along with other controls. I wanted to implement a functionality where a user can click a button to open the table's...
0
alishazel
by: alishazel | last post by:
## hi all im using microsoft access normally is i use similar coding it wont be any problem ... im a new perl user i already run in MDB the sql query have no problem... please over look for me what i...
13
by: sheldonlg | last post by:
I could use a little help here. I have goodled with not much luck. I have been at this for a bit and would appreciate suggestions. I have a form (A.php) that collects a lot of data. When I...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.