forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Console I/O routines

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
bplevenson



Joined: 02 Nov 2006
Posts: 21

PostPosted: Thu Nov 02, 2006 2:31 pm    Post subject: Console I/O routines Reply with quote

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
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 7916
Location: Salford, UK

PostPosted: Fri Nov 03, 2006 12:33 am    Post subject: Console I/O routines Reply with quote

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.
Back to top
View user's profile Send private message AIM Address
bplevenson



Joined: 02 Nov 2006
Posts: 21

PostPosted: Fri Nov 03, 2006 5:18 am    Post subject: Console I/O routines Reply with quote

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
Back to top
View user's profile Send private message
bplevenson



Joined: 02 Nov 2006
Posts: 21

PostPosted: Fri Nov 03, 2006 7:16 am    Post subject: Console I/O routines Reply with quote

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:
A) that you have to include win32prm.ins as well as win32api.ins
B) that the row and column are declared as integer*2, whereas the handle and "coord" item are integer*4
C) that you need to find the handle with the function GetStdHandle, which returns an Integer*4 value



Code:
!=====
! 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
Back to top
View user's profile Send private message
bplevenson



Joined: 02 Nov 2006
Posts: 21

PostPosted: Fri Nov 03, 2006 7:17 am    Post subject: Console I/O routines Reply with quote

Thanks to Paul Laidler in particular.

-BPL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group