Connecting Tech Pros Worldwide Forums | Help | Site Map

Help with Reopen file

Newbie
 
Join Date: Dec 2007
Posts: 5
#1: Dec 11 '07
In the first loop, i write all the lines with the word "clients" in them .

Now i want to go over "result.txt" from the begining and edit him again

How can i make RESULT handle to point to the start of file "result.txt" ?

Thanks :)
Expand|Select|Wrap|Line Numbers
  1. #!perl -w
  2. use strict; 
  3. use warnings;
  4.  
  5. my ($line,$file) ; 
  6.  
  7. # Open Results file
  8. open (RESULT,">c:/test/result.txt")  or die "Can't open result.txt\n"; ; 
  9. binmode RESULT;
  10.  
  11. open (RF,"<c:\test\log.txt") or die "Can't open log.txt\n";;
  12. binmode RF;
  13.  
  14. while ($line = <RF>) {
  15.  print RESULT $line if ($line=~m/clients/i);
  16. }
  17.  
  18.  

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Dec 11 '07

re: Help with Reopen file


the seek() function should work:

seek (RESULT,0,0);

add it where you need to return to the beginning of the file.

This line in your code might cause a problem:

open (RF,"<c:\test\log.txt")

\t in a double-quoted string is a tab, use forward slashes like you did earlier in your code to avoid this problem.
Newbie
 
Join Date: May 2007
Posts: 6
#3: Dec 13 '07

re: Help with Reopen file


Hi

What kevin said is indeed right, but if you want to use a simple feature , you can open file again in read mode n operate upon it.
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 901
#4: Dec 25 '07

re: Help with Reopen file


sagiv,

Did you ever get your problem worked out? A reply would be nice.

--Kevin
Kelicula's Avatar
Expert
 
Join Date: Jul 2007
Posts: 169
#5: Dec 25 '07

re: Help with Reopen file


Quote:

Originally Posted by KevinADC

the seek() function should work:

seek (RESULT,0,0);

add it where you need to return to the beginning of the file.

This line in your code might cause a problem:

open (RF,"<c:\test\log.txt")

\t in a double-quoted string is a tab, use forward slashes like you did earlier in your code to avoid this problem.


Yes unless you are typing within backticks perl automatically translates the slashes, no need to use backslashes....

No matter what platform you are running
Reply