Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

write and read the same file not display the output

by ulaksh (Acolyte)
on Sep 04, 2012 at 22:41 UTC ( #991695=perlquestion: print w/ replies, xml ) Need Help??
ulaksh has asked for the wisdom of the Perl Monks concerning the following question:

I am not able to see the output when I write the file and read the same file.
use strict; use warnings; open my $fo, '+>>', "outfile.txt" or die "Error:$!\n"; print $fo "how r u\n"; print $fo "hey suresh their\n"; print $fo "in bangalore\n"; while (my $line = <$fo>){ print "$line"; my $a =substr ($line, "suresh"); print "$a\n"; }
please let me know where I am doing the mistake

Comment on write and read the same file not display the output
Download Code
Re: write and read the same file not display the output
by choroba (Abbot) on Sep 04, 2012 at 22:44 UTC
    After writing to the file, the position in the filehandle corresponds to the end of the file. There is nothing to read there. You have to seek to the beginning of the file, or to a position obtained earlier by tell.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: write and read the same file not display the output
by Kenosis (Priest) on Sep 04, 2012 at 23:47 UTC

    Another option is to close the file, and then reopen it for reading:

    use strict; use warnings; open my $fo, '+>>', "outfile.txt" or die "Error:$!\n"; print $fo "how r u\n"; print $fo "hey suresh their\n"; print $fo "in bangalore\n"; close $fo; open my $fi, '<', "outfile.txt" or die "Error:$!\n"; while ( my $line = <$fi> ) { print "$line"; my $a = index( $line, "suresh" ); print "$a\n"; } close $fi;

    Output:

    how r u -1 hey suresh their 4 in bangalore -1 how r u -1 hey suresh their 4 in bangalore -1

    I'm assuming your meant index instead of substr.

      thanks for the help

      Now I am trying in different way, please correct if I am wrong

      while ( <$fi> ) { #print "$_"; my $a = $1 if /(^$name$)/; print $a; } close $fi;

      But when I run the file output show nothing, and it shows me some message, I am not getting where is the issue please see the below output

      perl readwritefile.pl Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 1. Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 2. Use of uninitialized value $a in print at readwritefile.pl line 15, <$ +fi> line 3.

        Hello ulaksh, Im guessing youve commented out your print "$_"; on purpose there. Anyway your getting yelled at because when the regex doesnt make a match, $a is not getting defined at all, so it is still undefined. You then try to use it on the next line print $a; You cant print $a because it has no value. It is undefined at the moment. This is also the reason your not getting anything printed to screen. maybe you can try this instead? :

        print $1 if /^($name)$/;
Re: write and read the same file not display the output
by Marshall (Prior) on Sep 05, 2012 at 04:21 UTC
    What you are attempting is an extremely bad idea.

    A much better general procedure is:
    - open the input file
    - make a new output file
    - when you are finished, rename the orignal file to .bak or whatever and then rename the new file to the original name.

    File system operations are complex and there is possibility when your program "bombs" or other errors happen and you will be left in an "unrecoverable situation" - the original data has been screwed up and there is maybe even no way, even with advanced techniques to recover the data.

    Basically trying to modify an existing file is a very advanced and rarely used technique. I have software that does that but the problems and pitfalls are many - don't do it!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991695]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-11-24 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (147 votes), past polls