home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5795 < prev    next >
Encoding:
Internet Message Format  |  1992-09-09  |  1.3 KB

  1. Path: sparky!uunet!wupost!sdd.hp.com!usc!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: problems opening a file read/write
  5. Message-ID: <1992Sep9.162322.7492@netlabs.com>
  6. Date: 9 Sep 92 16:23:22 GMT
  7. References: <1992Sep9.094428.16363@uvaarpa.Virginia.EDU>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 33
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <1992Sep9.094428.16363@uvaarpa.Virginia.EDU> krm%pri-mu.LOCAL@pri-mu.prime.com writes:
  14. : Hi net,
  15. : I tried the following small piece of code:
  16. : --------------------------------
  17. : #!/bin/perl
  18. : open(TEST,"+>tst")||die "open";
  19. : while($_=<TEST>){
  20. :    if (/add/){
  21. :       chop ;
  22. :       $_.="ADD\n";
  23. :    }
  24. :    print TEST $_ || die "print";
  25. : }
  26. : close(TEST) || die "close";
  27. : --------------------------------
  28. : Now the problem:
  29. : If I use open with "+>filename", the file will end up empty.
  30.  
  31. That's because +> will truncate a file, just like >.  The + only opens
  32. it for both reading a writing.  If you want to append to a file,
  33. use +>>.
  34.  
  35. : If I use "+<filename", it will remain unchanged.
  36.  
  37. This is probably a buglet in your stdio.  Many versions of stdio require
  38. a seek between reads and writes.  If you seek to the end of the file
  39. it should append.
  40.  
  41. Larry
  42.