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

INDEX() and nul terminated strings

gelarimer
Beginner
430 Views

Given the nul terminated string,
'C:\MyDir\MyProg.exe'C, I would like to
obtain the string 'C:MyDir'//ACHAR(0).

INTEGER iPos
CHARACTER(100) szPath

szPath = 'C:\MyDir\MyProg.exe'C

iPos = INDEX(szPath, '', BACK = .TRUE.)

! remove 'MyProg.exe'
szPath = szPath(1 : iPos-1)//ACHAR(0)

What result does the code above produce?

szPath = 'C:\MyDir'//ACHAR(0)

or

szPath = 'C:MyDir'//ACHAR(0)

Also, is there a way to get VS 2003 to display the Esc characters
in a nul terminated string. Currently VS displays
'C:\MyDir\MyProg.exe'C as 'C:MyDirMyProg.exe'

Thanks for any commentsinformation

0 Kudos
2 Replies
anthonyrichards
New Contributor III
430 Views

I think you are under the misapprehension that when you write

szPath='C:MyDirMyProg.exe'C

the extra backslashes are stored. That is not what happens.
The extra backslashes are used by the compiler to generate
the c-string 'C:MyDirMyProg.exe' with a null terminating character.
You can check this by testing the length of the string.
You will find that up to, but not including,the terminating null, it is
19 characters long, not 21.

So, given your code, you will finish up with the second result,
namely

szPath = 'C:MyDir'//ACHAR(0)

0 Kudos
gelarimer
Beginner
430 Views

Thanks Anthony.

If 'C:MyDirMyProg.exe'is returned by aWin32function such as GetModuleFileName(NULL, szPath, MAX_PATH), I assume the same is true, and that the code above would produce 'C:MyDir'//ACHAR(0). Is this correct?

Trying to troubleshoot a memory access violation that appears to be related to the way I manipulate nul terminated strings, but not sure.

0 Kudos
Reply