During my output flow, I was wondering if there is a way to put the cursor at the top left of the screen
(rather than scrolling the output)
If I reinitialize the screen BEFORE it reaches the bottom, it wont scroll, right ?
The reason for doing that is to get a dynamic readable flow of the output, which I will NOT
get if I scroll it.
Actually I dont HAVE to clear the remainder of the screen, I might just be able to place the cursor at the top left every 20 lines or so.
If you could refer me to a relevant article, that would be GREAT.
use kernel32 type(T_COORD) :: coord integer(HANDLE) :: hConsole ... hConsole = GetStdHandle(STD_OUTPUT_HANDLE) coord%X = 0 coord%Y = 0 if(SetConsoleCursorPosition(hConsole, coord) .eq. .false.) stop "error"
The above is untested code.
I cut and pasted that from Mr. Dempsey, but it does not do anything to the cursor position.
No error, either. Its a No OP.
apparently these articles are all in C++, so is there any way to do that with fortran?
Or maybe I have to go to QUickwin instead?
A trivial variiation of Jim's code works for me.
PROGRAM MoveTheCursor USE KERNEL32 IMPLICIT NONE TYPE(T_COORD) :: coord INTEGER(HANDLE) :: hConsole PRINT "('This is where text appears before')" hConsole = GetStdHandle(STD_OUTPUT_HANDLE) coord = T_COORD(0, 0) IF (SetConsoleCursorPosition(hConsole, coord) == 0_BOOL) STOP "error" PRINT "('This is where text appears after')" END PROGRAM MoveTheCursor
>>During my output flow, I was wondering if there is a way to put the cursor at the top left of the screen
Note, you did not ask to clear the screen too. While you can open kernel32.f90 and ifwinty.f90 (compiler\include folder) and examine the interfaces that sound somewhat like you need, you may find it easiest to perform a Google search:
windows console clear
While that example is in C++, you will likely find all the system call interfaces listed in kernel32.f90 or ifwinty.f90, then using them write a Fortran analog. (I do not think you will need to muck with the text attribute)
What we are trying to do is to show you how to research a problem and solve it yourself.
I still dont know how to reference these C++ routines from a Fortran program.
I noticed there were many others with that same issue - maybe an experienced C++ user would know better.
No articles about that topic?
Note that the MSDN link provided by Jim Dempsey has C calls. You don't need to know, how to call C++ code, in order to use that MSDN example.
Doesn't the sample code provided by IanH do what you need?
>>I still dont know how to reference these C++ routines from a Fortran program.
The Fortran interfaces to the Windows routines are located in kernel32.f90 and ifwinty.f90 (compiler\include folder).
A complete description of the Microsoft Windows routines are listed on the Microsoft websites:
The examples on the Microsoft website are generally C/C++ and some in C#. The Fortran modules use the same interface declarations as are available in the C/C++ examples. Fortran uses
structName%memberVariable ! Intel Fortran also supports . (but is non-standard/non-portable)
whereas C/C++/C# uses
You may run across some exceptions, some due to the fact that Fortran does not have unsigned integers.
IanH made some very useful suggestions, in particular the substitution for 0_BOOL due to C/C++ considering int(0) as a bool false, and anything else as a bool true. Fortran is quite different, Fortran looks at only the least significant bit. lsb=0=.false., lsb=1=.true. IOW C int(2) would be read as true in C, and .false. in Fortran. Note, reading the Microsoft example, return value, said a return of 0/false indicated error. IOW in Fortran you would use 0_BOOL to indicate failure (anything else is assumed to be true).
I too had the need for things like this, so I experimented and developed the attached code which does some of the things you asked for. It turns out that the API console routines--as referred to by several posters above--can do a lot of useful things and are simple to use if you just have some examples (in Fortran!) to get started.
The attached demo program (although not pretty) will set the cursor at a prescribed location, clear a block on the screen, clear the entire screen, and set the text color. I hope you find it helpful.