Kbase P103869: 4GL/ABL: IMPORT statement appears to silently fail when the source is an empty file
Autor |
  Progress Software Corporation - Progress |
Acesso |
  Público |
Publicação |
  1/22/2009 |
|
Status: Verified
SYMPTOM(s):
4GL/ABL: IMPORT statement appears to silently fail when the source is an empty file
The code after the DO WHILE TRUE loop statement in this procedure will never execute regardless of whether the input stream file c:\wrk91e\alpha.txt is empty or not:
DEFINE VARIABLE cLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFileName AS CHARACTER NO-UNDO.
DEFINE VARIABLE lFileEmpty AS LOGICAL NO-UNDO.
DEFINE STREAM OutputStream.
DEFINE STREAM InputStream.
/* Generates an empty file of size zero */
OUTPUT STREAM OutputStream TO "c:\wrk91e\alpha.txt".
PUT STREAM OutputStream.
OUTPUT STREAM OutputStream CLOSE.
/* Process the file if it is not empty */
ASSIGN
cFileName = "c:\wrk91e\alpha.txt"
lFileEmpty = TRUE.
INPUT STREAM InputStream FROM VALUE(cFileName).
DO WHILE TRUE:
IMPORT STREAM InputStream UNFORMATTED cLine.
END.
MESSAGE lFileEmpty
VIEW-AS ALERT-BOX INFO BUTTONS OK.
FACT(s) (Environment):
Code after a DO WHILE TRUE loop that reads a file using the IMPORT statement never executes
All Supported Operating Systems
Progress 9.x
OpenEdge 10.x
CAUSE:
The IMPORT statement raises the ENDKEY condition when we reach the end of an input file. The default processing of the of the ENDKEY condition is to UNDO, LEAVE the nearest REPEAT, FOR EACH or PROCEDURE block. Since there is no REPEAT or FOR EACH block involved here, the PROCEDURE block is subjected to the default ENDKEY processing of UNDO, LEAVE. Hence, no statements are executed after the ENDKEY condition is raised by the IMPORT statement.
FIX:
Use one of the following solutions:
1. Enclose the IMPORT statement is a REPEAT block to localize the default UNDO, LEAVE processing of the the ENDKEY condition to that block:
REPEAT:
IMPORT STREAM InputStream UNFORMATTED cLine.
END.
2. Enclose the IMPORT statement is a DO ON ENDKEY UNDO, LEAVE: block to localize the default UNDO, LEAVE processing of the the ENDKEY condition to that block:
DO ON ENDKEY UNDO, LEAVE:
IMPORT STREAM InputStream UNFORMATTED cLine.
END.
3. Use the FILE-INFO System Handle FILE-NAME and FILE-SIZE attributes to trap for empty or non existing files before using the IMPORT statement:
FILE-INFO:FILE-NAME = cFileName.
IF FILE-INFO:FILE-NAME <> ? AND FILE-INFO:FILE-SIZE > 0 THEN DO:
DO ON ENDKEY UNDO, LEAVE:
IMPORT STREAM InputStream UNFORMATTED cLine.
END.
END.
4. Use the SEEK statement and SEEK function to trap for empty files before using the IMPORT statement:
IF SEEK(InputStream) > 0 THEN DO:
SEEK STREAM InputStream TO 0.
DO ON ENDKEY UNDO, LEAVE:
IMPORT STREAM InputStream UNFORMATTED cLine.
END.
END.