Reading Macintosh Sequential Files Randomly and Vice Versa (20573)



The information in this article applies to:

  • Microsoft QuickBASIC 1.0
  • Microsoft QuickBASIC 1.0a
  • Microsoft QuickBASIC 1.0b
  • Microsoft BASIC Compiler
  • Microsoft BASIC Interpreter for Apple Macintosh 2.0
  • Microsoft BASIC Interpreter for Apple Macintosh 2.1
  • Microsoft BASIC Interpreter for Apple Macintosh 3.0

This article was previously published under Q20573

SUMMARY

This information applies to Microsoft QuickBASIC Versions 1.00, 1.00a, 1.00b, Microsoft BASIC Compiler Version 1.00, and Microsoft BASIC Interpreter Versions 2.00, 2.10, and 3.00 for the Apple Macintosh.

To read with Sequential access from a file created with Random access, you must put a carriage-return character, CHR$(13), at the end of each Random record. In Sequential access, each record is delimited with a carriage-return character, and records can have different lengths.

To read with Random access from a file created with Sequential access, all the records must have the same length; otherwise, the records will not line up properly into the fielded variables. Each record in a file created sequentially will have the number of characters written to it plus an extra byte for the return CHR$(13) at the end. The record length specified in the random OPEN statement should thus reflect the number of characters in the record plus a byte for the return.

MORE INFORMATION

If you are going to use the INPUT #n or LINE INPUT #n statement to read a file created with Random access, the file should only contain alphanumeric strings, and NO non-ASCII binary information. Otherwise, records may be delimitted prematurely by binary numeric data because a binary value of ASCII 13 may be in a byte of an integer or floating-point number.

If the file contains binary numeric data, then sequential input from a Random file may be done with the INPUT$() function. For example, if the record size is 128 bytes, then the following statements will read the first record into x$, leaving the file ready to read the next record, as follows:
   OPEN f$ FOR INPUT AS #1
   x$ = INPUT$(128,1)
				
You also could read in the records a piece at a time. For example, if the file is made up of records consisting of two integers, a single precision value, and an 80-character string, the following program shows how to read the first record of the file:
    OPEN f$ FOR INPUT AS #1
    id% = CVI( INPUT$(2,1) )
    count% = CVI( INPUT$(2,1) )
    price! = CVS( INPUT$(4,1) )
    descr$ = INPUT$(80,1)
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB20573