zero termination does not work

This commit is contained in:
Martin Diehl 2018-08-22 18:09:17 +02:00
parent 1a943df97e
commit dc596e6789
1 changed files with 12 additions and 13 deletions

View File

@ -85,23 +85,22 @@ character(len=1024) function getCWD()
C_NULL_CHAR C_NULL_CHAR
implicit none implicit none
character(kind=C_CHAR), dimension(1024) :: strFixedLength ! C string is an array character(kind=C_CHAR), dimension(1024) :: charArray ! C string is an array
integer(C_INT) :: stat integer(C_INT) :: stat
integer :: i integer :: i
call getCurrentWorkDir_C(strFixedLength,stat) call getCurrentWorkDir_C(charArray,stat)
if (stat /= 0_C_INT) then if (stat /= 0_C_INT) then
getCWD = 'Error occured when getting currend working directory' getCWD = 'Error occured when getting currend working directory'
else else
getCWD = repeat('',len(getCWD)) getCWD = repeat('',len(getCWD))
do i=1,1024 ! copy array components until Null string is found arrayToString: do i=1,len(getCWD)
if (strFixedLength(i) /= C_NULL_CHAR) then if (charArray(i) /= C_NULL_CHAR) then
getCWD(i:i)=strFixedLength(i) getCWD(i:i)=charArray(i)
else else
getCWD(i:i)=char(0)
exit exit
endif endif
enddo enddo arrayToString
endif endif
end function getCWD end function getCWD
@ -117,22 +116,22 @@ character(len=1024) function getHostName()
C_NULL_CHAR C_NULL_CHAR
implicit none implicit none
character(kind=C_CHAR), dimension(1024) :: strFixedLength ! C string is an array character(kind=C_CHAR), dimension(1024) :: charArray ! C string is an array
integer(C_INT) :: stat integer(C_INT) :: stat
integer :: i integer :: i
call getHostName_C(strFixedLength,stat) call getHostName_C(charArray,stat)
if (stat /= 0_C_INT) then if (stat /= 0_C_INT) then
getHostName = 'Error occured when getting host name' getHostName = 'Error occured when getting host name'
else else
getHostName = repeat('',len(getHostName)) getHostName = repeat('',len(getHostName))
do i=1,1024 ! copy array components until Null string is found arrayToString: do i=1,len(getHostName)
if (strFixedLength(i) /= C_NULL_CHAR) then if (charArray(i) /= C_NULL_CHAR) then
getHostName(i:i)=strFixedLength(i) getHostName(i:i)=charArray(i)
else else
exit exit
endif endif
enddo enddo arrayToString
endif endif
end function getHostName end function getHostName