Kbase P60876: 4GL/ABL: How to read the last line from a text file using 4GL?
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  12/14/2009 |
|
Status: Verified
GOAL:
4GL/ABL: How to read the last line from a text file using 4GL?
FACT(s) (Environment):
All Supported Operating Systems
Progress 8.x
Progress 9.x
OpenEdge 10.x
FIX:
The following procedure demonstrates how to read the last line from a Windows or a Unix text file:
DEFINE VARIABLE cLastLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE iSeek AS INTEGER NO-UNDO.
INPUT FROM "<path>;\<filename>".
/* Go to end of file. */
SEEK INPUT TO END.
/* Get the position before last end-of-line (CHR(10) on UNIX, CHR(10) + CHR(13) on Windows. */
IF OPSYS = "UNIX" THEN
ASSIGN iSeek = SEEK(INPUT) - 2.
ELSE
ASSIGN iSeek = SEEK(INPUT) - 3.
/* Now find the end-of-line before the last one. */
block-read:
DO WHILE iSeek >= 1:
SEEK INPUT TO iSeek.
READKEY.
IF (LASTKEY = 10 OR LASTKEY = 13) THEN LEAVE block-read.
ASSIGN iSeek = iSeek - 1.
END.
/* Once found, IMPORT the line as usual. */
IMPORT UNFORMATTED cLastLine.
INPUT CLOSE.
/* There we go! */
MESSAGE cLastLine
VIEW-AS ALERT-BOX INFO BUTTONS OK.