Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29010 Discussions

Is it possible to read the last line written on the console?

OP1
New Contributor III
1,184 Views
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
0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,184 Views
No - the consoile contents are not readable once written.
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,184 Views
In Windows...


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
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,184 Views
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
0 Kudos
OP1
New Contributor III
1,184 Views
Thanks Steve and Jim. I will try Jim's suggestion.

Olivier
0 Kudos
anthonyrichards
New Contributor III
1,184 Views
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
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,184 Views
Great post Anthony!

Jim Dempsey
0 Kudos
anthonyrichards
New Contributor III
1,184 Views
You're welcome!
0 Kudos
Reply