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

Need help with openFile

Well this is broken, I can't get it to open the file even though it does exist.

Expand|Select|Wrap|Line Numbers
  1. #!c:\Perl\bin\Perl.exe
  2. use CGI qw/:standard/;
  3. use constant TRUE => 1;
  4. use constant FALSE => 0;
  5. use warnings;
  6.  
  7. my $fileName = "guessingGame.csv";
  8. my $numGuesses;
  9. my $randomNumber;
  10. my $number = param('guessedNumber');
  11.  
  12. print header,
  13.     start_html('Guessing Game'),
  14.         h3('Pick a number between 1 & 100'),
  15.         start_form,
  16.             "Number: ",textfield('guessedNumber'),p,
  17.             submit,
  18.         end_form;
  19.     hr;
  20.  
  21.     if(param())
  22.     {
  23.         if(validateGuess())
  24.         {
  25.             print "Your guess is: ",em(escapeHTML($number)),p,
  26.             hr;
  27.         }
  28.     }
  29.  
  30.     print end_html;
  31.  
  32. sub validateGuess()
  33. {
  34.     if(($number == /^\d+$/) ||($number <= 0 )|| ($number > 100))
  35.     {
  36.         print "Please enter a digit between 0 and 101";
  37.         return FALSE;
  38.     }
  39.     else
  40.     {
  41.         $randomNumber = getRandomNum();
  42.         return TRUE;
  43.     }
  44. }
  45.  
  46. sub getRandomNum()
  47. {
  48.     load();
  49.     my $generatedNumber = int(rand(101));
  50.     print "\nGenerated Number:  $generatedNumber\n";
  51.     return $generatedNumber;
  52. }
  53.  
  54. sub load()
  55. {
  56.     open (FH, $fileName) || die(print "Could not open file!");
  57.     while (<FH>) 
  58.     {
  59.         chomp;
  60.         print "$_\n";
  61.         print "$randomNumber";
  62.     }
  63.     close (MYFILE); 
  64. }
  65.  
Feb 24 '09 #1
1 1664
KevinADC
4,059 Expert 2GB
write the line like this:

open (FH, $fileName) or die "Could not open file: $!";


$! will return the operating system error ('file not found' or whatever the error is).

The file must be in the same directory as the script since you are only using the filename to open it with, or you must change directory to where the file is before opening it or you could use the full disk path to the file in the open() function.
Feb 24 '09 #2

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

Similar topics

2
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call to tkFileDialog.askopenfilename() seems to screw...
2
by: Harlin | last post by:
I have an array of Appnames. Let's say they are 'Monkeys', 'Cats', 'Birds'. I would like create a button array with these: ---Start Code--- # Constructing and displaying buttons for a in...
5
by: Rob | last post by:
Help me, I'm just beginning with programming in Access 2000. I've tried the http://www.mvps.org/access/api/api0001.htm but it won't work in Access. What am i doing wrong. I don't have...
1
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
2
by: asenthil | last post by:
Hai... i'm triying to open a file in read mode using the following method.. HFILE hFile; hFile=OpenFile("D:\ss.doc",lpReOpenBuff,OF_READ); i cant undestand what is the 2nd parameter is... ...
3
stealwings
by: stealwings | last post by:
Hello everyone, I'm kind of dummy in this thing of programing but I have to do it so. Well I have this problem; I wrote a read and write program, it reads the file well but when it writes is it only...
2
by: vbaDev | last post by:
Hi. I am using Access 2000 and in my code I'm exporting a table into an Excel file (creating it), then the code needs to export another query into the same file (a new worksheet). So I needed both a...
3
by: matthewfrance7 | last post by:
This code is supposed to open a specific file, in this case an MP3 file but then show it in a list box. But it isn't working!! Private Sub btnAddFile_Click(ByVal sender As System.Object, ByVal e...
1
by: Sejoro | last post by:
I fixed the operand problem (thanks) and now a new one has risen up. I have to compile it with a makefile which I have made and I am getting more strange errors I don't know how to fix. Well,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...

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.