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

How to allow the user to view a txt file via the Command Line

P: 37
Hi
I am trying to allow the user to view the contents of a txt file via the
command line.
When I run the following script, pressing enter after naming my txt file
(which is in the same directory as the myscript.pl file I am running)
Nothing happens. I don't even get an error message.
Any assistance would be welcome!
Expand|Select|Wrap|Line Numbers
  1. # prompt the user for the file
  2.  
  3. print "Which file? ";
  4. $fileName = <>;
  5. chomp $fileName;
  6.  
  7. # open a file hanndle
  8.  
  9. open WORKING, $fileName;
  10.  
  11. # pull in contents of the file
  12.  
  13. @stats = stat $fileName;
  14. read WORKING, $contents, $stats[7];
  15.  
  16. # show the contents to the user
  17.  
  18. print $contents;
  19.  
Jan 10 '08 #1
Share this Question
Share on Google+
6 Replies


KevinADC
Expert 2.5K+
P: 4,059
change this line:
Expand|Select|Wrap|Line Numbers
  1. open WORKING, $fileName;
to:

Expand|Select|Wrap|Line Numbers
  1. open WORKING, $fileName or die "CAn't open $fileName: $!";
run the code and report back.
Jan 10 '08 #2

P: 37
Hi

I have just tried what you suggested and got the following error message,
even though employee.txt is in the same directory as the script.

C:\Documents and Settings\Setup>C:\perl\programs\myscript.pl
Which file? employee.txt
Can't open employee.txt: No such file or directory at C:\perl\programs\myscript.pl line 11, <> line
1.

C:\Documents and Settings\Setup>
Jan 11 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Hi

I have just tried what you suggested and got the following error message,
even though employee.txt is in the same directory as the script.

C:\Documents and Settings\Setup>C:\perl\programs\myscript.pl
Which file? employee.txt
Can't open employee.txt: No such file or directory at C:\perl\programs\myscript.pl line 11, <> line
1.

C:\Documents and Settings\Setup>
employee.txt is in C:\perl\programs\? Looks like it should work if thats the case.
Jan 11 '08 #4

P: 37
Hi
Thanks for the help you've given up to now.

I actually think my command line has been slightly corrupted.
My computer dealer suggested that I should re-install Windows.
Although that is'nt convenient right now.
I nead to be able to run perl scripts that interact with txt files.

Could you or anyone recommend any alternative command line software.
I found a program online called Top Perl Studio it says it can run
perl scripts and also has a built in http server.
Do you think that's the way to go?

Max
Jan 11 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
Hi
Thanks for the help you've given up to now.

I actually think my command line has been slightly corrupted.
My computer dealer suggested that I should re-install Windows.
Although that is'nt convenient right now.
I nead to be able to run perl scripts that interact with txt files.

Could you or anyone recommend any alternative command line software.
I found a program online called Top Perl Studio it says it can run
perl scripts and also has a built in http server.
Do you think that's the way to go?

Max

This one is free:

http://www.perl-express.com/

No http server but you said you wanted to run scripts as command line scripts, not CGI scripts. You still have to have perl installed to run it, as you do with any perl IDE software.
Jan 12 '08 #6

P: 37
Hi

Thanks for the link!
Perl Express is great, and free!

Regards Max
Jan 14 '08 #7

Post your reply

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