Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
updating a file

Posted by baby_perl (baby_perl), 5 March 2005
hello,
pls what sign can i use to open a file for modifying  rather than copyng the update to a new file, i want the same file to exhibit the updates

secondly i have this code below
open(INFILE, "topic2.txt") || die "cant find it";
open(OUTFILE, ">new2.txt") || die "cant find it";

while ($line = <INFILE>) {
 chomp $line;
 $line =~ s/ {1,}/\n/gi;
 print OUTFILE "$line\n";
}
close(INFILE) || die "cant close it";
close(OUTFILE) || die "cant close it";

the output is



the



quick

brown
fox

jumped
over

the

lazy

dog

pls how can i squeeze the space

baby perl

Posted by admin (Graham Ellis), 6 March 2005
on 03/05/05 at 09:14:33, baby_perl wrote:
hello,
pls what sign can i use to open a file for modifying  rather than copyng the update to a new file, i want the same file to exhibit the updates


If you open a file using "<+ at the start of the file name, you'll open it for read but also with write capability. Simarly you can use >+ and >>+ to open initially in write and append modes.
But beware - you probably don't want to do any of these

1. If you write to a file opened in one of these modes, you'll overwrite existing content byte by byte.  That's OK for fixed length record data, but such data is rare these days.  Replace a line with a shorter line, and you'll leave "droppings". Replace a line with a longer one, and you'll overwrite the start of the next line.

2. If you open with >+, any existing content is delete by the open.

3. You need to use the seek function to move around in the file and you MUST seek when you switch between read and write operations to flush buffers.

Better alternative scheme - write a NEW output file, then use Perl's rename function to move the old file to a .bak copy, and the new file to replace the old one.  Once you're 100% happy that you application is working, you could replace the first of those two renames with an unlink to get rid os the old file at that stage.

Quote:
secondly i have this code below
open(INFILE, "topic2.txt") || die "cant find it";
open(OUTFILE, ">new2.txt") || die "cant find it";

while ($line = <INFILE>) {
 chomp $line;
 $line =~ s/ {1,}/\n/gi;
 print OUTFILE "$line\n";
}
close(INFILE) || die "cant close it";
close(OUTFILE) || die "cant close it";

the output is



the



quick

brown
fox

jumped
over

the

lazy

dog

pls how can i squeeze the space

baby perl


Add something like
        if ($line =~ /\S/)
onto the end of your print line (before the semicolon).  It will then read
         print OUTFILE "$line\n" if ($line =~ /\S/);

That means "print out the line but only if it contains visible (not white space) characters"



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho