Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
29 Views

Closing a file after using USEROPEN

I have used the USEROPEN specifier in the OPEN statement to create a file. In opening this file the full path has been stripped out.

So I can open the file, but the manual warns me:

‘You may get unexpected results if you specify OPEN with a filename and a USEROPEN specifier that opens a different filename, and then use a CLOSE statement with STATUS=DELETE (or DISPOSE=DELETE). In this case, the run-time library assumes you want to delete the file named in the OPEN statement, not the one you specified in the USEROPEN function.’

This is true. I cannot close the file using the close(unit=……) statement.

Does anyone have any ideas as to how I can do this?

0 Kudos
10 Replies
Highlighted
Black Belt Retired Employee
29 Views

Then don’t use STATUS=‘DELETE’. If you want to delete the file, close the unit normally, then use the Windows API to close the handle and delete the file.

0 Kudos
Highlighted
Beginner
29 Views

Thanks Steve, as always.

I am using the example from the documentation for USEROPEN. This creates the file via:
  ! Do the CreateFile( ) call and return the status to the Fortran rtl
  STS = CreateFile(strippedName, DESIRED_ACCESS, SHARE_MODE, NULL_SEC_ATTR, CREATE_DISP, FLAGS_ATTR, 0)

 So how can I get the handle?

 

0 Kudos
Highlighted
Valued Contributor III
29 Views

In your example STS is the handle, note this is integer(handle) :: sts  as is is either 32 or 64 bit dependant on your build.

 

0 Kudos
Highlighted
Black Belt Retired Employee
29 Views

The documentation should have said "return value" rather than "status". As Andrew says, CreateFile returns a handle (or zero if unsuccessful), and the USEROPEN routine returns this to the Fortran library so that it knows which handle to use for I/O operations. You may want to save this somewhere else (module variable, for example) in case you want to use it later.

0 Kudos
Highlighted
Beginner
29 Views

Thanks Steve and andrew_4619. I can now close handles and delete files. 

However, if I delete a handle or a file, Fortran obligingly tries to do it again when I exit the program and I get an error message of the form:

forrtl: severe (28): CLOSE error, unit -129, file "Unknown"

Is there a way of ignoring this error and continuing?

The code (which is just me experimenting) is enclosed.

0 Kudos
Highlighted
Valued Contributor III
29 Views

maybe close in fortran  with an error status variable  CLOSE(unit_var, IOSTAT=ierr_var) which you can then ignore as no hard error will be created. 

0 Kudos
Highlighted
Black Belt Retired Employee
29 Views

If what you want is to delete the file, do that (with DeleteFile) and then do a Fortran CLOSE on the unit.

0 Kudos
Highlighted
Beginner
29 Views

Thanks Steve, but I still get the same error.

0 Kudos
Highlighted
Black Belt Retired Employee
29 Views

Even if you closed the unit? Can you attach a small example demonstrating the problem? Your earlier reply suggested there was an attachment but I don't see it.

0 Kudos
Highlighted
Beginner
29 Views

Thanks Steve. It turns out that I don't need to close the handle. I use close() then DeleteFile if I wan't to delete the file. So I think everything is now OK. (?)

0 Kudos