Reading real number from editbox

Reading real number from editbox

Hello, I have a small problem. I would like to read a number from editbox, so I:
1) read the text into a character*256 variable (Xtext) by this command:
...
retlog=getdlgitemtext(hdlg,IDC_EDIT_MZ1,Xtext,256)
...
(When I debug it I can see the text (0.200) in the variable Xtext)

2) and I try to read this text into a real*8 variable (zlomek):
...
READ(Xtext,*,iostat=bcteni) zlomek
...
but zlomek remains 0.0E0

If you know how to manage to read it, help me please

Thank you

Vit Meistr

15 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

The following code, pulled from a large collection of
Win32 wrapper functions, shows what you want.

SUBROUTINE EditBoxGetReal (hwnd, controlID, argval, nulldefault)
IMPLICIT NONE
INTEGER, INTENT(IN) :: hwnd
INTEGER, INTENT(IN) :: controlID
REAL,INTENT(IN),OPTIONAL :: nulldefault
REAL,INTENT(INOUT) :: argval

INTEGER, PARAMETER :: maxchars = 40
CHARACTER(LEN=maxchars) :: contents
REAL :: value
INTEGER :: status
INTEGER :: pos, hour, minute

CALL EditBoxReadText (hwnd, controlId, contents, maxchars)

! stop now if no data to convert
pos = INDEX(contents, CHAR(0))
IF (is_empty(contents, pos)) THEN
IF (PRESENT(nulldefault)) argval = nulldefault
RETURN
END IF

CALL CStringToFortran (contents)
contents = ADJUSTL(contents)

! allow ":" as a timefield delimiter
pos = INDEX(contents, ':')
status = 1
IF (pos > 0) THEN
contents(pos:pos) = ' '
READ (contents, *, IOSTAT = status, END=5) hour, minute
IF (status == 0) THEN
SELECT CASE (minute)
CASE (0:59)
argval = FLOAT(hour) + FLOAT(minute)/60.
END SELECT
END IF
RETURN

! regular real value
ELSE
IF (INDEX(contents, '.') == 0) THEN
pos = INDEX(contents, ' ')
contents(pos:pos) = '.'
END IF
READ (contents, '(F)', IOSTAT=status, END=5) value
IF (status == 0) argval = value
END IF

5 RETURN

END SUBROUTINE EditBoxGetReal

What does your iostat variable bcteni contain after the READ executes?

Mike

I was watching this too and the value of bcteni is .true.
The problem (I think) is that I need to convert variable Xtext (Cstring) into normal fortran character string. Is there any routine doing this? I couldn't find the routine CStringToFortran, that is in paulcurtis's example anywhere.

Usually what one does is use INDEX to find the offset of the terminating NUL character and then using a substring to make it look like a "Fortran string". I'm guessing that the READ is hitting the NUL and complaining.

Steve

Steve - Intel Developer Support

Here is the way I usually do IT:

subroutine MySub()
use dflib
use dflogm
implicit none
include 'resource.fd'
character str*256
integer ret
real x
ret=dlgget(dlg,Edit1,str) ! dlg is a DIALOG type variable
read (str,*) x ! Read real x from str
...
end subroutine

You can also use GetWindowText:

use dfwina
...
call GetWindowText(dlg.hWnd,str,256) ! Use API to read the text
str=str(1:scan(str,char(0))) ! Convert to Fortran character type

Good Luck!

Aah, I did't know about this INDEX function.
Works fine, Thank you

Vit

Could you share what code ended up working for you? Thanks.

I tried to attach packed file, but I couldn't manage. So here is the code.

!Function reads numbers from editbox
!and transform it to real number
REAL*4 FUNCTION EditToReal4(hDlg,id)
USE DFWIN
IMPLICIT NONE

INCLUDE 'resource.fd'

INTEGER hDlg,id
CHARACTER*256 Xedit,Xchar
LOGICAL retlog

retlog=getdlgitemtext(hdlg,id,Xedit,256)
CALL CstringToChar (XEdit,Xchar)
READ(Xchar,*,iostat=retlog) EditToReal4
RETURN
END

!SUBROUTINE to convert a NULL terminated
!string into fortran string
SUBROUTINE CstringToChar (XCstring,Xchar)

character*256 XCstring,Xchar
integer*4 position

position=INDEX(xCstring,CHAR(0))
Xchar=XCstring(1:position-1)

RETURN
END

I'm getting this type error, do you know why?

Error: This name does not have a type, and must have an explicit type. [EDITTOREAL4]
antwidth = EditToReal4(hDlg,IDC_WIDTH)

Because you did not declare that EditToReal4 is a REAL FUNCTION.

You should really do some reading on Language Reference Manual -- especially "Program Units and Procedures"/"Procedure interfaces".

Jugoslav

Jugoslav
www.xeffort.com

I thing you have to put an interface block into the routine where are you calling EditToReal4 function from. Just put the following part of code before declarations. It should work.

INTERFACE
REAL*4 FUNCTION EditToReal4 (hdlg,id)
INTEGER hdlg,id
END FUNCTION
END INTERFACE

Vit

VIT PLEASE STOP!

Your posts (7 of them by the time I'm writing this) are visible to everyone -- but you. This is an old Forum bug which rears its ugly head again.

Steve, please try to get this fixed.

Jugoslav

Jugoslav
www.xeffort.com

I am very sorry for messing this thread up. I didn't know about this bug, I'll remember.
sorry
Vit

I've deleted the extra posts.

I suspect the "missing posts" problem is due to a browser cache issue, with the forum server not properly reporting that the page has been updated. I'll try to explain this to the forum engineers.

Steve

Steve - Intel Developer Support

Leave a Comment

Please sign in to add a comment. Not a member? Join today