Reading from Fixed length records

Name Type Size
Surname Text 30
Name Text 20
age INT 4

"Smith                          John                20Hamlett                       Mr                  22"

The above file can be read in by using the record structure. It does not matter what the data is in a fixed length record as long as you know the structure.

file = OPEN("myFLR.data", R)
sname = READ(file, 30)
name = READ(file, 20)
age = READ(file, 4)
CLOSE(file)

sname = "Smith                         "
name = "John                "
age = 20

We got the exact data we put in. We do have some excess spaces so we must trim them off using the procedure TRIM which will remove all spaces from the end of some text. This leaves us with the final code -

file = OPEN("myFLR.data", R)
sname = TRIM( READ(file, 30) )
name = TRIM( READ(file, 20) )
age = READ(file, 4)
CLOSE(file)

sname = "Smith"
name = "John"
age = 20

What if we wanted to read in the second record? The second record starts at position 54. So if we read in the first 54, but ignore the data, we will be able to access the second record. We could also just skip over the first 54 bytes by using a method such as SKIP.

file = OPEN("myFLR.data", R)
SKIP(file, 54)
sname = TRIM( READ(file, 30) )
name = TRIM( READ(file, 20) )
age = READ(file, 4)
CLOSE(file)

sname = "Hamflett"
name = "Mr"
age = 22

Note - In most typed langauages there are different read and write proceudres for different data types. As such you must know the order of types in order to be able to read the file correctly.