Input from different files (simultaneously)

Hello everyone, hope you are doing well!!

I want to retrieve all the data from two different files (abc & abc1 in our example) at the same time.

If I do something like that (please refer below code for this) then I could get only first line from both of the files and if I surround input from with repeat block then it would be an infinite kind of loop.


Code:
def var s-path as char init "c:\rajat\abc.txt" no-undo.
def var s-path2 as char init "c:\rajat\abc1.txt" no-undo.

def var l-line  as char no-undo.
def var l-line2 as char no-undo.

/* if I use REPEAT block here then it’s going into an infinite loop */
input from value(s-path).
import unformatted l-line.
message l-line 
      view-as alert-box.
input from value(s-path2).
import unformatted l-line2.
message l-line2 
      view-as alert-box.
input close.


I thought that we could achieve this by using different streams, I had been trying to do the same with streams but faced the same problem as mentioned above.

Code:
def var s-path as char init "c:\rajat\abc.txt" no-undo.
def var s-path2 as char init "c:\rajat\abc1.txt" no-undo.

def var l-line  as char no-undo.
def var l-line2 as char no-undo.

def stream s-abc.
def stream s-abc1.

/* if I use REPEAT block here then it’s going into an infinite loop */
input stream s-abc from value(s-path).
import stream s-abc l-line.
message l-line view-as alert-box.
input stream s-abc1 from value(s-path2).
import stream s-abc1 l-line2.
message l-line2 view-as alert-box.
/*Closing streams*/

Please suggest.

Thanks & Regards!
Rajat.
 

GregTomkins

Active Member
You definitely want to be using one stream per file (that you want to read simultaneously). I can't explain why either example would trigger an infinite loop. I believe the standard behaviour is that when EOF occurs, the REPEAT automagically exits. I have seen my share of infinite loops, but I don't think ever with REPEAT ... IMPORT...
 

Stefan

Well-Known Member
Code:
def var cpath as char no-undo extent 2 init ["c:\temp\import.one.txt", "c:\temp\import.two.txt" ]. 
def var cline as char no-undo extent 2.

def stream sone.
def stream stwo.

input stream sone from value(cpath[1]).
input stream stwo from value(cpath[2]).

def var lreading as logical no-undo initial true extent 2.

do while lreading[1] or lreading[2] on endkey undo, retry:

   cline = "".  
   
   if lreading[1] then do on error undo, leave:
      import stream sone cline[1].
      catch e as progress.lang.error:      
         lreading[1] = false.              
      end catch.
   end.
       
   if lreading[2] then do on error undo,leave:        
      import stream stwo cline[2].
      catch e as progress.lang.error:            
         lreading[2] = false.            
      end catch.
   end.
         
   message
      cline[1] skip
      cline[2]
   view-as alert-box.
end.    

close stream sone.
close stream stwo.
 
Top