- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If what you want is to delete the file, do that (with DeleteFile) and then do a Fortran CLOSE on the unit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve, but I still get the same error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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. (?)

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page