Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Processing Files with variable length (binary) records

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Processing Files with variable length (binary) records
#13365
Posted by: PeterJ 2010-09-21 18:52:49 Last edited by: PeterJ 2010-09-21 19:01:00 (Total edited 3 times)
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")
/* Create a random record variable length */
DO i=1 TO hi
   record=COPIES(i,RANDOM(1,100))
   writevarrec(file,record)
END 
closevarrec(file)
/* Now read all records */ 
openvarrec(file,"READ")
DO FOREVER
   record=readvarrec(file)
   IF record=-4 THEN LEAVE 
   SAY record 
END 
closevarrec(file)
EXIT
/* ----------------------------------------------------------------------------------
 * Write Variable length Record, format: "length(4 bates)record"
 * ----------------------------------------------------------------------------------
 */
writevarrec: PROCEDURE
PARSE ARG file,record
VALUEOUT(file,LENGTH(record),,2)
RETURN CHAROUT(file,record)
/* ----------------------------------------------------------------------------------
 * Read Variable length Record, format: "length(4 bates)record"
 * ----------------------------------------------------------------------------------
 */
readvarrec: PROCEDURE
PARSE ARG file
len=VALUEIN(file,,2)
IF len='' THEN RETURN -4
RETURN CHARIN(file,,len)
/* ----------------------------------------------------------------------------------
 * Close file format (Variable length Record) 
 * ----------------------------------------------------------------------------------
 */
closevarrec: PROCEDURE
PARSE ARG file
RETURN CHAROUT(file)
/* ----------------------------------------------------------------------------------
 * Close file format (Variable length Record) 
 * ----------------------------------------------------------------------------------
 */
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
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-3-28  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0