Home: Perl Programming Help: Frequently Asked Questions:
How come when I open a file read-write it wipes it



Jasmine
Administrator

Jan 26, 2001, 10:19 AM


Views: 3406
How come when I open a file read-write it wipes it

(From the Perl FAQ)

How come when I open a file read-write it wipes it out?

Because you're using something like this, which truncates the file and then gives you read-write access:

Code
    open(FH, "+> /path/name");          # WRONG (almost always)

Whoops. You should instead use this, which will fail if the file doesn't exist. Using ``>'' always clobbers or creates. Using ``<'' never does either. The ``+'' doesn't change this.

Here are examples of many kinds of file opens. Those using sysopen() all assume

Code
    use Fcntl;

To open file for reading:

Code
    open(FH, "< $path")                                 || die $!; 
sysopen(FH, $path, O_RDONLY) || die $!;

To open file for writing, create new file if needed or else truncate old file:

Code
    open(FH, "> $path") || die $!; 
sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT) || die $!;
sysopen(FH, $path, O_WRONLY|O_TRUNC|O_CREAT, 0666) || die $!;

To open file for writing, create new file, file must not exist:

Code
    sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT)         || die $!; 
sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT, 0666) || die $!;

To open file for appending, create if necessary:

Code
    open(FH, ">> $path") || die $!; 
sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT) || die $!;
sysopen(FH, $path, O_WRONLY|O_APPEND|O_CREAT, 0666) || die $!;

To open file for appending, file must exist:

Code
    sysopen(FH, $path, O_WRONLY|O_APPEND)               || die $!;

To open file for update, file must exist:

Code
    open(FH, "+< $path")                                || die $!; 
sysopen(FH, $path, O_RDWR) || die $!;

To open file for update, create file if necessary:

Code
    sysopen(FH, $path, O_RDWR|O_CREAT)                  || die $!; 
sysopen(FH, $path, O_RDWR|O_CREAT, 0666) || die $!;

To open file for update, file must not exist:

Code
    sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT)           || die $!; 
sysopen(FH, $path, O_RDWR|O_EXCL|O_CREAT, 0666) || die $!;

To open a file without blocking, creating if necessary:

Code
    sysopen(FH, "/tmp/somefile", O_WRONLY|O_NDELAY|O_CREAT) 
or die "can't open /tmp/somefile: $!":

Be warned that neither creation nor deletion of files is guaranteed to be an atomic operation over NFS. That is, two processes might both successful create or unlink the same file! Therefore O_EXCL isn't so exclusive as you might wish.