if records contain binary information LINEIN/LINEOUT is not the best choice to read/write to a file. LINOUT adds CRLF (0D0A) as line end information, LINEIN treats CRLF as line end identifyer. But 0D0A might also represent a binary number of 3338 contained in the middle of your record. LINEIN will split the record at this position and return a corrupted record.
if we add 2 bytes record length in front of each record, we can read the record length first and then read exactly the bytes belonging to the record (using CHARIN). I added a couple procedures, which simplify this read/write process:
file='C:\temp\myfile.txt'
hi=100
openvarrec(file,"WRITE")
DO i=1 TO hi
record=COPIES(i,RANDOM(1,100))
writevarrec(file,record)
END
closevarrec(file)
openvarrec(file,"READ")
DO FOREVER
record=readvarrec(file)
IF record=-4 THEN LEAVE
SAY record
END
closevarrec(file)
EXIT
writevarrec: PROCEDURE
PARSE ARG file,record
VALUEOUT(file,LENGTH(record),,2)
RETURN CHAROUT(file,record)
readvarrec: PROCEDURE
PARSE ARG file
len=VALUEIN(file,,2)
IF len='' THEN RETURN -4
RETURN CHARIN(file,,len)
closevarrec: PROCEDURE
PARSE ARG file
RETURN CHAROUT(file)
openvarrec: PROCEDURE
PARSE ARG file,mode
IF mode='' THEN mode='READ'
mode=TRANSLATE(mode)
IF mode='READ' THEN DO
IF STREAM(file, 'C', 'OPEN READ') == 'READY:' THEN RETURN 0
ELSE SAY file 'not opened (Read Mode)'
END
ELSE IF mode='WRITE' THEN DO
IF STREAM(file, 'C', 'OPEN WRITE REPLACE') == 'READY:' THEN RETURN 0
ELSE SAY file 'not opened (Write Mode)'
END
ELSE IF mode='APPEND' THEN DO
IF STREAM(file, 'C', 'OPEN WRITE APPEND') == 'READY:' THEN RETURN 0
ELSE SAY file 'not opened (Append Mode)'
END
RETURN 8
|