473,320 Members | 2,146 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,320 software developers and data experts.

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
4 2655
eWish
971 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
4,059 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
971 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

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

Similar topics

0
by: gilgantic | last post by:
I get an "Invalid file" in my MIDlet when I tried to read a .txt file, using Connection.open(). My phone is a Nokia 3595 with mMode technology by AT&T. Here is my source code in my MIDlet: try...
0
by: GAK | last post by:
Hello, Can anyone help me to generate a regular expression for a file that looks like this: "-rw-r--r-- 1 ftponly 38 Jul 12 11:20 testfile.ctl" its failing to validate with this...
6
by: Rolf Schroedter | last post by:
(Sorry for cross-posting). I need to access large files > 2GByte (Linux, WinXP/NTFS) using the standard C-library calls. Till today I thought I know how to do it, namely for Win32: Use open(),...
3
by: trellow | last post by:
Hello, I am writing an application that needs to read a file that is already open by another process for writing. When I do the following: FileStream fs = new FileStream(fileName,...
11
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
2
by: vignesh1985 | last post by:
hi, Anybody can u pls help me out how to cleanup the gaps between to data's in text file by using regular expression using perl programming.
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.