- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hallo,
I use the function shown below to load textfiles to a buffer for later to show the text in a text box.
Usually it works fine, but in some cases the file can not be opened (handle hFile=0) and the no text is read. As a test I have tried
to open and the close the file ( by the standard Open /Close Fortran statements) before I apply the CreateFile function to ensure the file is
not locked somwhere else in the program. Open/close is ok, but the CreateFile function do not always succed in opening it.
When I open the textfile with Notepad, it always looks OK.
Looking forward to get a clue from somebody out there....
Reidar
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, &
NULL, OPEN_EXISTING, 0, NULL)
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm not sure what's going on here. Could you try changing the dwFlagsAndAttributes argument from 0 to perhaps FILE_ATTRIBUTE_NORMAL or FILE_ATTRIBUTE_READONLY? Probably won't do any good, though. Could you show us how you set the value of szFileName? That seems to be the most likely problem, for example if you forgot to include the trailing ACHAR(0). Also, when the function fails print out the value returned by GetLastError() because that can be illuminating sometimes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[fortran]
FUNCTION open_the_file (fullpath, rwmode) RESULT (ihandl)
IMPLICIT NONE
INTEGER(HANDLE) :: ihandl
INTEGER :: access
CHARACTER(LEN=*),INTENT(IN) :: fullpath,rwmode
! Win32 file opens
IF (rwmode == 'R') THEN
access = GENERIC_READ
ELSE
access = IOR(GENERIC_READ,GENERIC_WRITE)
END IF
ihandl = CreateFile (fullpath, &
access, &
FILE_SHARE_READ, &
NULL_SECURITY_ATTRIBUTES, &
OPEN_ALWAYS, &
FILE_ATTRIBUTE_NORMAL, &
NULL )
IF (ihandl == INVALID_HANDLE_VALUE) THEN
! deal with failure
END IF
END FUNCTION open_the_file
[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
if(len_trim(fullpath) == len(fullpath)) stop "No room for NULL" fullpath(len_trim(fullpath):len_trim(fullpath)) = ACHAR(0) ! now do your CreateFile
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you for for your response,
I modified the file name field as shown below:
hFile = CreateFile(trim(szFileName)//char(0), GENERIC_READ, FILE_SHARE_READ, &
and now it works fine,
Reidar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To remove potential confusion of future reader of your code, I would suggest removing "sz" from szFileName as this indicates it is a zero (NULL) terminated string.
hFile = CreateFile(trim(fullpath)//char(0), GENERIC_READ, FILE_SHARE_READ, &
Would not be confusing, and may in fact be what you have in your code.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Jim, yes, that's true, the zs could be confusing...
BR
Reidar

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