in reply to reading text from afile

On unix it would seem that you are truncating the text files by opening them to write before you have read them, a result of opening them by the same name. I don't recall how that works on win32, but I suspect it is the same.

You can fix that by either using a temporary file name and renaming it after the copy is done, or else by opening in '+<' mode, which opens to read and write, without truncation.

Here's a rewrite using the non-truncating r/w open. I prefer it to the temporary file because of race conditions which may occur with temporary files. I'll use glob to simplify grabbing the file listing.

#!/usr/bin/perl use warnings; use strict; use Fcntl ':flock'; my $path = 'c:/perl/dvd_files/'; # only doing the problem files - those with unchanged names my @files = glob "$path*.txt"; for (@files) { local $/; open my $fh, '+<', $_ or warn($!), next; flock $fh, LOCK_EX; binmode $fh; my $text = <$fh>;
Your following substitution looks fishy. I can't tell what you really want to do, but what it does can be done by tr///,
$text =~ tr/0-9{}//d; seek $fh, 0, 0; truncate $fh, 0; print $fh $text; }
The code for the other files is similar, but there is not the issue of clobbering the file you are reading from. There is an issue of clobbering a text file if a .sub or .srt file has the same basename as a text file.

On reflection, I wonder if you wouldn't be better off creating the rewritten files in a brand-new subdirectory and not clobbering anything. That may be necessary, anyway, since I can't test on win32 and am not sure that flock, seek, or truncate will work on your system.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: reading text from afile
by fadingjava (Acolyte) on Sep 28, 2004 at 08:24 UTC
    thanks zaxo , i agree with you that i overdid somethings. your code seems to work much better for me . i have another query though . this code that i am posting now is taken from a site for stripping HTML . It does the same thing that my other code was doing , generated empty txt files and gives me "Use of uninitialised variable " error for $plain_text. can any body tell me why??
    #!c:/perl/perl.exe use strict; use warnings; use CGI ':standard'; use HTML::Parser; my $plain_text ; my $p = HTML::Parser->new(text_h => [\&text_rtn, 'text']); my $path = "c:/perl/htmlfiles"; opendir(LOCAT, $path) or die "Couldn't open folder, $!\n"; my @folder = grep !/^\.\.?$/, readdir(LOCAT); closedir (LOCAT); # Do a loop for each file in the folder. This gets the filename also. foreach my $file (@folder) { my $full_path = $path.$file; print "Reading '$full_path'\n"; $p->parse_file($full_path); $full_path =~ s/html/txt/gi; $full_path =~ s/htm/txt/gi; print "Writing '$full_path'\n"; open(WRITE,">$full_path") or die("Cannot create file!"); print WRITE $plain_text; close(WRITE); } sub text_rtn { foreach (@_) { $plain_text .= "$_\n"; } }