Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

CreateFile will not always open an excisting file

reidar
New User
996 Views

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)

0 Kudos
6 Replies
JVanB
Valued Contributor II
996 Views

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.

0 Kudos
Paul_Curtis
Valued Contributor I
996 Views

[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]

0 Kudos
jimdempseyatthecove
Honored Contributor III
996 Views
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

0 Kudos
reidar
New User
996 Views

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

0 Kudos
jimdempseyatthecove
Honored Contributor III
996 Views

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

0 Kudos
reidar
New User
996 Views

Thanks Jim, yes, that's true, the zs could be confusing...

BR

Reidar

0 Kudos
Reply