Geeting Attempt to read from closed stream Datafile(1386) Er

Posted by Krishna Kumar on 20-Oct-2016 09:38

Hi ,

I have written below program in order to take data from .csv file and search whether the specific word(present in .csv) is available in all .p/w / .I files which are present in a specific directory. In case word is present then  I'm outputting current directory, current file and line number to other.csv file.  However once I run it I'm getting below error -

"  Attempt to read from closed stream Datafile(1386) Error "" . Can you plese check and let me know how to resolve the same.

Attached is the .csv file.

Below is the code -  

DEFINE VARIABLE cDirectoryList   AS CHARACTER NO-UNDO.
DEFINE VARIABLE cCurrentDiretory AS CHARACTER NO-UNDO.
DEFINE VARIABLE cCurrentFile     AS CHARACTER NO-UNDO.
DEFINE VARIABLE iCounterVariable AS INTEGER   NO-UNDO.
 
DEFINE STREAM   DirStream.
DEFINE STREAM   FileStream.
DEFINE STREAM   ReportStream. 
DEFINE STREAM Datafile.  /* kk */


 ASSIGN cDirectoryList = "E:\kk".
 
DO iCounterVariable = 1 TO NUM-ENTRIES(cDirectoryList):
    ASSIGN
        cCurrentDiretory = ENTRY(iCounterVariable, cDirectoryList).
    INPUT STREAM DirStream FROM OS-DIR( cCurrentDiretory).
    REPEAT:
        IMPORT STREAM DirStream cCurrentFile.
        IF LENGTH(cCurrentFile) < 3 THEN NEXT.
        IF cCurrentFile MATCHES "*~~.p" OR
            cCurrentFile MATCHES "*~~.w" OR
            cCurrentFile MATCHES "*~~.i"  THEN
                RUN ParseThisFile(INPUT cCurrentFile, INPUT cCurrentDiretory).
    END.
    INPUT STREAM DirStream CLOSE.
END.
 
PROCEDURE ParseThisFile:
    DEFINE INPUT PARAMETER ipcCurrentFile     AS CHARACTER   NO-UNDO.
    DEFINE INPUT PARAMETER ipcCurrentDiretory AS CHARACTER   NO-UNDO.
 
    DEFINE VARIABLE cLine                     AS CHARACTER   NO-UNDO.
    DEFINE VARIABLE iOuterLineCounter         AS INTEGER     NO-UNDO.
    DEFINE VARIABLE iInnerLineCounter         AS INTEGER     NO-UNDO.
    DEFINE VARIABLE cStatement                AS CHARACTER   NO-UNDO. 
    DEFINE VARIABLE i AS INTEGER NO-UNDO.

    DEFINE VARIABLE text-string AS CHARACTER FORMAT "x(76)".
   

    INPUT STREAM FileStream FROM VALUE (ipcCurrentDiretory + "\" + ipcCurrentFile).
    INPUT STREAM Datafile FROM VALUE ("C:\files\test.csv").
    OUTPUT STREAM ReportStream TO VALUE ("C:\files\krishna.txt")APPEND.
    REPEAT:
        IMPORT STREAM FileStream UNFORMATTED cLine.
        ASSIGN
            iOuterLineCounter = iOuterLineCounter + 1
            cStatement = TRIM(cLine).


 REPEAT : 
 
IMPORT STREAM Datafile UNFORMATTED text-string.
 
IF  text-string = " " OR  cstatement = " "  THEN NEXT .

   
IF NOT LOOKUP(cstatement,entry(2,text-string,",")," ") > 0  THEN NEXT.

        iInnerLineCounter = iOuterLineCounter.
        DO WHILE SUBSTRING(cStatement, LENGTH(cStatement)) <> '.' AND SUBSTRING(cStatement, LENGTH(cStatement)) <> ':':
            IMPORT STREAM FileStream UNFORMATTED cLine.
            iInnerLineCounter = iInnerLineCounter + 1.
            cStatement = cStatement + " " + TRIM(cLine).
        END. /* DO WHILE */
        iOuterLineCounter = iInnerLineCounter.
 
 
        PUT STREAM ReportStream UNFORMATTED
            ipcCurrentDiretory AT 1
            ipcCurrentFile AT 52
            iOuterLineCounter AT 104
            cStatement AT 110 SKIP. 
     
    END. /* REPEAT */ 
  
    END .   /* kkstream */ 
  
    INPUT STREAM FileStream CLOSE.
    OUTPUT STREAM ReportStream CLOSE.
 
  /* INPUT  STREAM Datafile CLOSE. */
END PROCEDURE.

Posted by gdb390 on 20-Oct-2016 10:04

this is because you repeat the import stream datafile for every line in the outer repeat , the first line of the file will be ok, the second line crashes because there are no more lines left in de datafile stream

Solution

instead of reading the datafile stream in the repeat of the filestream, take it out and put the values in a temp-table

use that temp-table in the repeat block of the filestream

All Replies

Posted by gdb390 on 20-Oct-2016 10:04

this is because you repeat the import stream datafile for every line in the outer repeat , the first line of the file will be ok, the second line crashes because there are no more lines left in de datafile stream

Solution

instead of reading the datafile stream in the repeat of the filestream, take it out and put the values in a temp-table

use that temp-table in the repeat block of the filestream

Posted by Krishna Kumar on 21-Oct-2016 02:45

Thanks- It worked.

This thread is closed