ramadurai has asked for the wisdom of the Perl Monks concerning the following question:

i open txt file for i-o (+<filename) . when i update a field, the field does not reflect new values? why? i tried closing and opening the file inside the program. still it does not happen. the update happens only when i close the program and come back to dos prompt and run the program again. why? do we have flush facility like 'C' language? what else i should try? ramadurai r

Replies are listed 'Best First'.
Re: update a txt file
by PodMaster (Abbot) on Mar 14, 2004 at 01:15 UTC
    It's because you're doing something wrong, and without code, no one can tell you what. Anyway, Tie::File is nice
    use Tie::File; { tie my @array, 'Tie::File', 'filename' or die "eek $@ $! $/"; push @array, scalar localtime; unshift @array, scalar gmtime; } warn "and the file contains $/"; { tie my @array, 'Tie::File', 'filename' or die "eek $@ $! $/"; print $_,$/ for @array; } __END__ $$ perl test and the file contains Sun Mar 14 01:19:12 2004 Sat Mar 13 17:19:12 2004 $$ perl test and the file contains Sun Mar 14 01:19:14 2004 Sun Mar 14 01:19:12 2004 Sat Mar 13 17:19:12 2004 Sat Mar 13 17:19:14 2004 $$ perl test and the file contains Sun Mar 14 01:19:15 2004 Sun Mar 14 01:19:14 2004 Sun Mar 14 01:19:12 2004 Sat Mar 13 17:19:12 2004 Sat Mar 13 17:19:14 2004 Sat Mar 13 17:19:15 2004

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: update a txt file
by pbeckingham (Parson) on Mar 14, 2004 at 01:20 UTC

    Now the following works, and prints "read in 'xxx'". Given the file io.txt:

    onetwothreefour
    and the following code:
    #! /usr/bin/perl -w use strict; if (open FILE, '+<io.txt') { seek FILE, 3, 0; print FILE 'xxx'; seek FILE, 3, 0; my $input = ''; sysread FILE, $input, 3; print "read in '$input'\n"; close FILE; }
    But you really ought to have provided the misbehaving code.

Re: update a txt file
by Anonymous Monk on Mar 14, 2004 at 03:56 UTC
    You should be passing seek 0 (zero) as the second argument, not 5. You are missing a semi-colon after line 36, and there is no such module Term::ReadALine. My answersmake about as much sense as your question.