[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-win32-users
Subject:    Re: (no subject)
From:       Carl Jolley <cjolley () iac ! net>
Date:       2001-04-30 18:55:47
[Download RAW message or body]

On Mon, 30 Apr 2001, Ewen, Ed wrote:

> Hi all,
>   I have a weird situation on my hands.  I wrote code to create a file, and
> if there is an open error go to a routine that check to see if the directory
> exists and creates it if it doesn't.  The thing is when I run it in the
> debugger, I get a return code of blank on the open and then execute
> check_directory and all is well.  But without the debugger, the open RC is
> 1, even though the directory doesn't exist.  Anyone have any ideas?
> Thanks,
>
>
> sub check_directory {
> $hold_msg = $!;
> $rc = opendir(XXX,$op_dir);
> print "check_directory\n";
> if ($rc) {
> 	print MSG "\$msg .= \'Error creating file - $op_fn -
> $hold_msg\\n\'\;\n";
> 	close MSG;
> 	exit 4}
> $rc = mkdir("$op_dir",0777);
> 	if (!$rc) {
> 		print MSG "\$msg .= \'Error creating directory - $op_dir -
> $!\\n\'\;\n";
> 		close MSG;
> 		exit 4}
> $rc = open OPF, ">$op_fn";
> if (!$rc) {
> 	print MSG "\$msg .= \'Error creating file - $op_fn - $!\\n\'\;\n";
> 	close MSG;
> 	exit 4}
> print "success\n";
> return}
> sub process_hdr {
> if (!$expecting_hdr) {exit 8}
> $op_dir = $to_dir . substr($_,4,6);
> $op_fn = $op_dir . "\\$yymmdd.ctb";
> $rcx = open OPF, ">$op_fn" or check_directory;
> print "rcx is $rc\n";
> $expecting_hdr = 0;
> $plan_id_h = substr($_,4,6);
> print MSG "\$msg .= \'Creating file - $op_fn\\n\'\;\n";
> return }
> _______________________________________________

You seem to be under the impression that the opendir function will
create a directory and return true if it doesn't succeed. Both of these
assumptions are incorrect. The opendir function will try to open a
existing directory and return true if it succeeds. I suggest that you
modify your logic to check for the existance of the directory prior to
trying to open the file, e.g.

mkdir($op_dir,0777) unless -d $op_dir;
die "can\'t create directory $op_dir: $! at " unless -d $op_dir;

You should then try to open the file and produce an error message
it it doesn't succeed.

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic