- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I would like to know if the following is possible: is there a way in Fortran to read the previously output line of text on the console screen? I would like to know if that line was blank or not.
Thanks,
Olivier
I would like to know if the following is possible: is there a way in Fortran to read the previously output line of text on the console screen? I would like to know if that line was blank or not.
Thanks,
Olivier
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No - the consoile contents are not readable once written.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In Windows...
Jim Dempsey
ReadConsoleOutputCharacter
The ReadConsoleOutputCharacter function copies a number of characters from consecutive cells of a console screen buffer, beginning at a specified location.
BOOL ReadConsoleOutputCharacter( HANDLE hConsoleOutput, LPTSTR lpCharacter, DWORD nLength, COORD dwReadCoord, LPDWORD lpNumberOfCharsRead );
Parameters
- hConsoleOutput
- [in] Handle to a console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.
- lpCharacter
- [out] Pointer to a buffer that receives the characters read from the console screen buffer. The total size required will be less than 64K.
- nLength
- [in] Number of screen buffer character cells from which to read. The size of the buffer pointed to by the lpCharacter parameter should be
nLength * sizeof(TCHAR)
. - dwReadCoord
- [in] Coordinates of the first cell in the console screen buffer from which to read, in characters. The X member of the COORD structure is the column, and the Y member is the row.
- lpNumberOfCharsRead
- [out] Pointer to a variable that receives the number of characters actually read.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
If the number of characters to be read from extends beyond the end of the specified screen buffer row, characters are read from the next row. If the number of characters to be read from extends beyond the end of the console screen buffer, characters up to the end of the console screen buffer are read.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I should add that the characters to be read, must actually reside in the console output window.
IOW if FORTAN has the data buffered for eventual output, the read will not retrieve that which is not yet written.
Jim Dempsey
IOW if FORTAN has the data buffered for eventual output, the read will not retrieve that which is not yet written.
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve and Jim. I will try Jim's suggestion.
Olivier
Olivier
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have tested this code using IVF and it works for me.
Integer Function LastConsoleLine(consolechars)
!
! Obtains the location of the console cursor from the ConsoleScreenBufferInfo
! structure populated by a call to GetConsoleScreenBufferInfo
! Then uses the present location of the cursor (row and column values in a T_COORD structure)
! to obtain the contents of the previous row using ReadConsoleOutputCharacter,
! reading up to 80 characters and placing them into variable CONSOLECHARS.
! The function returns CONSOLECHARS and its length minus any trailing blanks.
!
use dfwin
use dfwinty
implicit none
integer lconsoleline, nconsolechars, hconsole, lret
type (T_COORD) coord
type (T_CONSOLE_SCREEN_BUFFER_INFO) consoleinfo
! This structure contains
! TYPE (T_COORD) dwSize
! TYPE (T_COORD) dwCursorPosition ! THIS GIVES THE CURSOR POSITION THAT IS WANTED
! integer(WORD) wAttributes
! TYPE (T_SMALL_RECT) srWindow
! TYPE (T_COORD) dwMaximumWindowSize
character(80) consolechars
hconsole=GetStdHandle(STD_OUTPUT_HANDLE)
! Get the console information
lret=GetConsoleScreenBufferInfo(hconsole,consoleinfo)
! Step back one row and start reading at column 1
coord%y=consoleinfo%dwcursorposition%y-1
coord%x=1
lret=ReadConsoleOutputCharacter(hconsole,consolechars,80,coord,LOC(nconsolechars))
lconsoleline=LEN_TRIM(consolechars)
! print *,'Last Console line contents = ',TRIM(consolechars)
! print *,'Last Console line length (minus trailing blanks)= ',lconsoleline
LastConsoleLine=lconsoleline
!
! If return value = 0, last line was all blanks
end Function
Integer Function LastConsoleLine(consolechars)
!
! Obtains the location of the console cursor from the ConsoleScreenBufferInfo
! structure populated by a call to GetConsoleScreenBufferInfo
! Then uses the present location of the cursor (row and column values in a T_COORD structure)
! to obtain the contents of the previous row using ReadConsoleOutputCharacter,
! reading up to 80 characters and placing them into variable CONSOLECHARS.
! The function returns CONSOLECHARS and its length minus any trailing blanks.
!
use dfwin
use dfwinty
implicit none
integer lconsoleline, nconsolechars, hconsole, lret
type (T_COORD) coord
type (T_CONSOLE_SCREEN_BUFFER_INFO) consoleinfo
! This structure contains
! TYPE (T_COORD) dwSize
! TYPE (T_COORD) dwCursorPosition ! THIS GIVES THE CURSOR POSITION THAT IS WANTED
! integer(WORD) wAttributes
! TYPE (T_SMALL_RECT) srWindow
! TYPE (T_COORD) dwMaximumWindowSize
character(80) consolechars
hconsole=GetStdHandle(STD_OUTPUT_HANDLE)
! Get the console information
lret=GetConsoleScreenBufferInfo(hconsole,consoleinfo)
! Step back one row and start reading at column 1
coord%y=consoleinfo%dwcursorposition%y-1
coord%x=1
lret=ReadConsoleOutputCharacter(hconsole,consolechars,80,coord,LOC(nconsolechars))
lconsoleline=LEN_TRIM(consolechars)
! print *,'Last Console line contents = ',TRIM(consolechars)
! print *,'Last Console line length (minus trailing blanks)= ',lconsoleline
LastConsoleLine=lconsoleline
!
! If return value = 0, last line was all blanks
end Function
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Great post Anthony!
Jim Dempsey
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You're welcome!

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page