in reply to open file, write then read problem
We are good at giving advice. ;)
First, always use strictures (quite likely you are in fact, but didn't include them in your sample).
Use the three parameter version of open and use lexical file handles:
open my $FILE1_TEMP, '+>', $file1_temp or die "Could not open $file1_t +emp: $!";
However, that is probably not the advice you want. Your code appends a string to a file then prints everything from the end of the file to, well, the end of the file - that is, nothing (as you have noticed).
The fix is either to close and reopen the file, or to seek to the start before your while loop:
use strict; use warnings; my $file1_temp = "delme.txt"; open my $ioFile, '+>', $file1_temp or die "Could not open $file1_temp: + $!"; print $ioFile "hello, world!"; seek $ioFile, 0, 0; print while <$ioFile>; close $ioFile;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: open file, write then read problem
by doniking (Novice) on Jun 15, 2009 at 03:44 UTC |