Silverfrost Forums

Welcome to our forums

Console I/O routines

2 Nov 2006 1:31 #1189

I'm only interested in console applications, and for the ones I have in mind, I need to be able to 1) clear the screen, and 2) print text at a particular row and column. There seems to be no way to do it with Salford Fortran. Is there any way to access the Windows API so I can use those console routines?

-BPL

2 Nov 2006 11:33 #1191

Barton

You can access the Windows API. You can use the supplied win32api.ins to give you the pattern. See also 'Calling C/C++ from FTN95' in the help file.

3 Nov 2006 4:18 #1195

Okay, I managed to get it to compile, but the cursor positioning routines (2 available, as far as I can tell from reading the include file) just don't work. Here's my code:


program console implicit none include 'win32api.ins'

integer row ! Row on screen, should be 1-25. integer col ! Column on screen, should be 1-80.

write(*, 10) 10 format('Row? ⇒', $) read *, Row

write(*, 20) 20 format('Col? ⇒', $) read *, Col

! Call SetConsoleCursorPosition(Col, Row) ! I hope this works.

Call SetCursorPos(Row, Col) print *, 'Hello from ', Row, ':', Col ! Print output to screen. end program console

And here's the output I get:


Row? ⇒10 Col? ⇒15 Hello from 15: 10

Press RETURN to close window . . .

Clearly I'm doing something wrong, but what?

-BPL

3 Nov 2006 6:16 #1197

Okay, I think I may have solved my own problem. The routine to use is the first of the two, SetConsoleCursorPosition. And its arguments are not the row and column, but...

1st argument: The Windows handle of the console window. 2nd argument: A composite containing the row and column.

Here's some code that works. Note:

  1. that you have to include win32prm.ins as well as win32api.ins

  2. that the row and column are declared as integer2, whereas the handle and 'coord' item are integer4

  3. that you need to find the handle with the function GetStdHandle, which returns an Integer*4 value

    !===== ! console is an attempt to set the cursor position from within Salford Fortran. !===== program console implicit none ! Must declare all variables. include 'win32api.ins' ! Routines. include 'win32prm.ins' ! Constants.

    integer*2 Col                       ! Column on screen, should be 0-79.
    integer*4 Coord                     ! Composite value of row and column.
    integer*2 Row                       ! Row on screen, should be 0-24.
    integer*4 HandleForConsole          ! From GetStdHandle routine.
    

    !-----

    write(*, 10)
    

    10 format('Row? ⇒', $) read *, Row

    write(*, 20)
    

    20 format('Col? ⇒', $) read *, Col

    Coord = 65536 * Row + Col            ! Jam 2 I2s into an I4.
    HandleForConsole = GetStdHandle(STD_OUTPUT_HANDLE)
    
    Call SetConsoleCursorPosition(HandleForConsole, Coord)    ! I hope this works.
    write(*, 30) Col, Row
    

    30 format('Hello from ', I2, ':', I2, '!') end program console

Thanks to all who pointed me in the right direction.

-BPL

3 Nov 2006 6:17 #1198

Thanks to Paul Laidler in particular.

-BPL

Please login to reply.