Random+access+files+with+arrays

Why this library
Here you'll find a number of functions that you can use as an alternative to ordinary //random// mode file I/O. The main difference is that here a single array is used for all the fields of a record. Apart from that the functions are designed to behave similarly to ordinary random access file I/O: fields that are shorter than the specified length of that field are padded with spaces, fields that are longer are truncated. toc The main motivation to create these functions is that JustBASIC does //not// allow the following syntax: code format="vb" open someFile$ for random as #f len = m field #f, 16 as A$(1),_ 16 as A$(2),_ ...   16 as A$(n) code However, having to use a separate string for each field makes it hard to process record entries using loops. Especially if a record contains a lot of fields, this may force the programmer to use lots and lots of code lines, where an array would allow to write just a three-line loop. If arrays could be used instead, you wouldn't have meaningful names for individual fields and it would be up to you to know what kind of data for example A$(4) contains, but on the positive side, you could in some cases greatly reduce the amount of code needed, and therefore also the number of potential typos and bugs.

General notes
All the functions in this collection use two arrays: **RecordField$(n)** and **FieldLength(n)**. It is the programmers responsibility do DIM these arrays at the beginning of the program. They will be automatically REDIMmed in the OpenRandomFile function.
 * RecordField$(n)** represents strings for individual fields in a record. Just as with ordinary random access I/O, you'd assign string values to RecordField$(1) - RecordField$(n) (where n is the number of fields you have declared) before calling the PutRecord function, and this is where the results of a GetRecord call are stored.
 * FieldLength(n**) should be used and modified only by the functions. //You should not modify its values yourself.// If you need, you can find some additional information from it: **FieldLength(0)** stores the number of total fields in one record, **FieldLength(n+1)** stores the size of one record in bytes/characters (it can be accessed like that //size = FieldLength(FieldLength(0)+1)// but there should be no practical need to do so.

Before using any of the functions **//OpenRandomFile//** should be called first. Only //one// file can be open at a time (since arrays are global). Before the program ends or before opening another file //**CloseRandomFile**// must be called. In a GUI database program, it is OK to open the database file at the beginning of the program, and not close it until in the //trapclose// branch.

Although most of these functions perform some sort of error checking (to prevent attempts to write or read beyond end of file) and return 0 if this should happen, you should not rely on this feature. As a good programmer it is your task to make sure that all those functions return 1 (success, no eof error). Of course, no error checking can prevent misuse of the functions and coding errors.

All these functions may be used without restrictions. You may modify them to suit your needs, or use them as they are. A source file containing the whole collection and examples of its usage, can be downloaded at [|Just Basic Files Archive]

Main functions
So OpenRandomFile("mydata.dat", "16 8 2") opens "mydata.dat" and sets up 3 fields: 16 characters, 8 characters and 2 characters respectively. You can now use RecordField$(1)...RecordField$(3). code format="vb" function OpenRandomFile(fileName$, fieldLengths$) while word$(fieldLengths$, fieldCount+1) <> "" fieldCount = fieldCount+1 wend redim RecordField$(fieldCount) redim FieldLength(fieldCount+1) FieldLength(0) = fieldCount for i = 1 to fieldCount FieldLength(i) = val(word$(fieldLengths$, i)) size = size+FieldLength(i) next i   FieldLength(fieldCount+1) = size open fileName$ for binary as #randomarray OpenRandomFile = lof(#randomarray)/size end function code
 * function OpenRandomFile(fileName$, fieldLengths$)
 * This function opens file called **fileName$** for random access file I/O. The second argument is a string containing the length of each field separated by a space. The function returns the total number of records in the file.

code format="vb" sub CloseRandomFile close #randomarray end sub code
 * sub CloseRandomFile
 * This sub closes the file opened with **OpenRandomFile**. Remember to call it before the program ends.

code format="vb" function PutRecord(no) for i = 1 to FieldLength(0) s$ = s$+left$(RecordField$(i)+space$(FieldLength(i)), FieldLength(i)) next i   pos = (no-1)*FieldLength(FieldLength(0)+1) if pos >= 0 and pos <= lof(#randomarray) then PutRecord = 1 seek #randomarray, pos print #randomarray, s$   end if end function code
 * function PutRecord(no)
 * This function saves the current contents of RecordField$( in the file as record number //no//. The function returns 1 if successful, or 0 if it failed because //no// would have been beyond end of file.

code format="vb" function GetRecord(no) pos = (no-1)*FieldLength(FieldLength(0)+1) if pos >= 0 and pos+FieldLength(FieldLength(0)+1) <= lof(#randomarray) then GetRecord = 1 for i = 1 to FieldLength(0) seek #randomarray, pos RecordField$(i) = input$(#randomarray, FieldLength(i)) RecordField$(i) = trim$(RecordField$(i)) pos = pos+FieldLength(i) next i   end if end function code toc
 * function GetRecord(no)
 * This function reads record number //no// from the file and writes it to array RecordField$(. The fields are automatically trimmed of all padding whitespace. The function returns 1 if successful, or 0 if //no// was beyond end of file.

Additional functions
In addition to the above main functions, there are several additional functions available. These functions are designed to facilitate some common tasks with database file I/O.

View additional functions