467,081 Members | 1,143 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Reopening file only if it was updated using regular open

Hello
I'm trying to reopen a certain file but only if it's updated:
The problem is when i try to open a file (after it was changed and saved by me)for reading second time, i get nothing, like the file is empty but it's not.
The filehandle is empty.Why?
DOn't have a clue.
The code is below.
can some one help?
Thanks
Expand|Select|Wrap|Line Numbers
  1. $last_acces=time;
  2. while($end_ctsc)
  3. {
  4. $full_path_file=lastmodifiedfile("path");
  5. open (DATA_log ,$full_path_file);
  6. $file_stat = stat(DATA_log);
  7. $new_acces=$file_stat->mtime;                
  8. close DATA_log;
  9. $Sub_is=$new_acces-$last_acces;
  10. if ($Sub_is)
  11. {
  12. $last_acces=$new_acces;
  13. $i=0;
  14. $full_path_file=lastmodifiedfile("path");
  15. open (DATA_log ,$full_path_file)||print("could not open the log");
  16. @file_log=<DATA_log>;
  17. close DATA_log;
  18. $sizefile_log = @file_log;
  19. print "the i is: $i, the sizefile_log is $sizefile_log\n";
  20. while ($i!=$sizefile_log)
  21. {
  22.   #file handling 
  23. }
  24. }
  25.  
Oct 27 '08 #1
  • viewed: 2442
Share:
4 Replies
eWish
Expert 512MB
After you open a file add a die statement after to make sure you did not have any problems.
Expand|Select|Wrap|Line Numbers
  1. open(my $file, '<', $file_to_open) || die "Can't open $file_to_open: $!\n";
--Kevin
Oct 27 '08 #2
Thanks for the response.
I used $file as file handle,had the same result:
First time it's working the second time it's empty.
When i write second time i mean it could 500 time because i open the file and reopen it to check if it was updated using stat function.The funny thing is that i'm able to recognize if it was updated or not using open with filehandle but when i want to spread the data to an array i get it empty.
Any ideas?
Oct 27 '08 #3
KevinADC
Expert 2GB
One potential problem is here:

Expand|Select|Wrap|Line Numbers
  1. $file_stat = stat(DATA_log);
the stat() function returns a list so you have to assign its value to a list or array:

Expand|Select|Wrap|Line Numbers
  1. @stat = stat(DATA_log);
Later you use $file_stat as if its an object:

Expand|Select|Wrap|Line Numbers
  1. $new_acces=$file_stat->mtime;
Are you using a module that creates an object?


You very much need to add "strict" and "warnings" to your perl program and note all the errors and fix them and post back here if you have more questions.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warniings;
  3. use diagnostics;
  4.  
diagnostics will return more verbose nessages concerning the errors or warnings but it is optional.
Oct 27 '08 #4
eWish
Expert 512MB
Here is a sample script that will create a test file if it does not exist. Then if you run it again it will modify the contents of the file and display the data.

When you want to append a file then you must open it in append mode. Otherwise it will erase all of the data.

Also, this sample script is intended to be run from a browser.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -T
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI::Carp qw/fatalsToBrowser/;
  7.  
  8. print "Content-type: text/html\n\n";
  9.  
  10. my $file_to_open = './sample_data_file.txt';
  11. my $modified_time  = (stat($file_to_open))[9];
  12.  
  13.  
  14. # If the target file does exists create it 
  15. if (not -e $file_to_open) {
  16.     open(my $fh, '>', $file_to_open) || die "Can't open $file_to_open.  Error Message: $!\n";    
  17.     print $fh "Some message\n";
  18.     close($fh);
  19.  
  20.     print 'File was created';
  21. }
  22.  
  23. # If modified then update it
  24. if ($modified_time) {
  25.     open(my $fh, '>>', $file_to_open) || die "Can't open $file_to_open.  Error Message: $!\n";    
  26.     print $fh "Some new message\n";
  27.     close($fh);    
  28.  
  29.     print 'File was modified', "<br /><br />";
  30. }
  31.  
  32. # Print modified contents
  33. if ($modified_time) {
  34.     open(my $fh, '<', $file_to_open) || die "Can't open $file_to_open.  Error Message: $!\n";
  35.     print "Modified Contents:<br />";
  36.     while (<$fh>) {
  37.         chomp;
  38.         print $_, "<br>";
  39.     }
  40.     close($fh);
  41. }
  42.  
  43.  
  44. 1;
  45.  
------------------------------------------------------------
Pragmas (perl 5.10.0) used :
  • strict - Perl pragma to restrict unsafe constructs
  • warnings - Perl pragma to control optional warnings
Core (perl 5.10.0) Modules used :
  • CGI::Carp - CGI routines for writing to the HTTPD (or other) error log

I just wanted to use this cool tool. :)

--Kevin
Oct 28 '08 #5

Post your reply

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

Similar topics

reply views Thread by gilgantic | last post: by
6 posts views Thread by Rolf Schroedter | last post: by
3 posts views Thread by James D. Marshall | last post: by
4 posts views Thread by Egyd Csaba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.