create new file

C

Cody Hess

I want my script to create new files when opening a filehandle to a file
that doesn't exist.

open FILE, "> new_file.dat" or die "Failed: $!";

I believe this should happen automatically, but I get the error

Failed: No such file or directory at ./write_file_experiment.pl line 3.

Is there something wrong with my filehandle? Is there another way to
create a new file if it doesn't already exist?
 
B

Bob Walton

Cody said:
I want my script to create new files when opening a filehandle to a file
that doesn't exist.

open FILE, "> new_file.dat" or die "Failed: $!";

I believe this should happen automatically, but I get the error

Failed: No such file or directory at ./write_file_experiment.pl line 3.

Is there something wrong with my filehandle? Is there another way to
create a new file if it doesn't already exist?

Your code, run verbatim, works fine on my system (Windoze 98SE, AS Perl
build 806). You don't say what your OS and Perl versions are -- would
you happen to be in a directory where the user you are running doesn't
have directory write (or maybe read) priviledge (and on an OS where that
is possible)?
 
G

Gunnar Hjalmarsson

Bob said:
would you happen to be in a directory where the user you are
running doesn't have directory write (or maybe read) priviledge
(and on an OS where that is possible)?

I had the same thought, but when testing it I got the "Permission
denied" error message, and not "No such file...".
 
C

Cody Hess

Bob said:
Your code, run verbatim, works fine on my system (Windoze 98SE, AS Perl
build 806). You don't say what your OS and Perl versions are -- would
you happen to be in a directory where the user you are running doesn't
have directory write (or maybe read) priviledge (and on an OS where that
is possible)?
I run perl 5.8 on Mac OS X (10.1). The directory is writable to the
owner. I've tried running the script as root and I get the same error
message.
 
K

ko

Cody Hess said:
I want my script to create new files when opening a filehandle to a file
that doesn't exist.

open FILE, "> new_file.dat" or die "Failed: $!";

I believe this should happen automatically, but I get the error

Failed: No such file or directory at ./write_file_experiment.pl line 3.

Works ok for me...
Is there something wrong with my filehandle? Is there another way to
create a new file if it doesn't already exist?

You can always use sysopen():

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

make sure you inlude 'use Fcntl;'. This will do what you asked - open
a file for writing, creating a new file which must not previously
exist. For details, 'perldoc perlopentut'.

HTH - keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top