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

Why has my program has stop writting to files.

P: 8
I wrote a program which writes informations to files and it was working fine, it created and wrote to over 1000 files, but now it will only write to files occasionaly. I don't know if it is an error with the program or the computer. I have looked at 'hot' filehandles I don't really understand how to add them to my code, but I don't think that is the answer. Please can someone advise me?
Expand|Select|Wrap|Line Numbers
  1. $cmd = system "perl $Program $filePath > $dir$fileName";              
  2.  
  3. $fileName2 = join ("\n", @Array);
  4.  
  5. open(NewFile, " > $dir$fileName.txt") || die "Cannot create a new file";
  6.   print NewFile $fileName2, "\n";
  7. close(NewFile);
  8.  
  9. open(NewFile, " >> "Progress.txt") || die "Cannot update the file";
  10.   print NewFile $value, "\n";
  11. close(NewFile);
  12.  
So it sends arguments to the command line these arguments are processed by another perl program "$Program", the arguments for the program are specified by "$filePath" and the file it writes to is "$dir$fileName". Note the file has already been created and the other program does open, print to and close the file correctly. But ocassionaly nothing appears in the file. This problem also occurs with the "$dir$fileName.txt" and yes the array does have values in it. The files that are used to write to are different every time, appart from "Progress.txt" which gets information added to on each cycle.
Jul 27 '10 #1
Share this Question
Share on Google+
6 Replies


Expert Mod 100+
P: 589
If the $dir$fileName file is empty after the system call, then that would indicate that either the $program failed to execute, or it executed but did not generate any output to STDOUT i.e., it probably had a fatal error prior to sending anything out to STDOUT.

If $dir$fileName.txt is being created, but is occasionally empty, then @Array is empty.

Are either of the die statements being executed?
Jul 27 '10 #2

P: 8
@RonB
Thank for your reply Ron

The $program works fine, well it has worked and printed to file with the arguments that are currently being passed to it, so I know that works.
The @Array isn't empty because the $filePath is generated from this, and I see the output on the terminal window, I see the $program processing the information in the terminal window also. the $value also does not print in the file and again has a value, not even the "\n" is printing in the file.
Yes the die statements are being executed, they have been tested.
Jul 27 '10 #3

Expert Mod 100+
P: 589
Yes the die statements are being executed
Then that's the reason the files aren't being written to. The die statement will only execute if the open call fails to open the file. You need to change the die statement to include the filename and reason it failed to open.

Also, you should be using the 3 arg form of open and a lexical var for the filehandle.

open my $NewFile, '>', "$dir$fileName.txt") || die "Cannot create <$dir$fileName.txt> $!";

Additionally, the program that you're executing via the system call should be modified to accept $dir$fileName as one of its parameters/arguments and that script should open and write to the file so that you could have proper error handling on that portion.
Jul 27 '10 #4

P: 8
@RonB
Sorry I misinterpreted the question. I thought you were asking if the die statements were working, they stop the program if there is an error in creating the file or opening the file, but there is not so they are created I see this happening. This code has already been used and it does write to files. I have ran this already and it has worked over 1000 times, but has now decided to stop. I will start using the 3 arg form that you have mentioned thank you.
The $program does accept $dir$fileName as it's last argument.
Jul 27 '10 #5

Expert Mod 100+
P: 589
The $program does accept $dir$fileName as it's last argument.
No it doesn't, not the way you have it written. Your Perl script is completely unaware of the $dir$fileName argument. You're using your shell to do output redirection and it is not doing any error handling.

How much free disk space do you have when it fails to write to the file(s)?

It is possible to open a file, but not write to it if the disk/volume is full. Add a die statement on the close statement to trap this type of issue. You could even add a die statement to the print statements, just to be sure.
Jul 27 '10 #6

P: 8
@RonB
Thanks again Ron.

The program does take $dir$fileName as it's last arguement, just the way it is written. Yes the problem is with the disk space I was allocated so problem solved, thank you it is working now.
Jul 28 '10 #7

Post your reply

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