268 Storing Varying Size Data in Untyped Files Random-access files make it easy to store and retrieve data. Because you retrieve only the data you want, random-access files don’t waste time reading the entire file. Storing Varying Size Data in Untyped Files Random-access files are great for storing chunks of data of equal size. However, what if you want to store data that may vary in size? You could define a record as the largest size data you need to store, but that means you wind up wasting space. As a more efficient alternative, you can store data in untyped files. Untyped files still organize data in records, but each record can vary in size, as shown in Figure 8-3. Figure 8-3: Untyped files contain records that can vary in size. Records can be any size in an untyped file. Writing data To store data in an untyped file, you must first name and create an untyped file. In the Delphi programming language, you can create an untyped file by declaring a variable as a File type, such as var myFile : File; After you create a variable name, you need to assign that variable name to an actual filename, such as AssignFile(myFile, ‘MyData.dat’); In some languages, such as BASIC, you assign a number to an actual filename. In Delphi and other languages, you assign a name to an actual file. The purpose of assigning a number or a name is so that you can refer to a file without typing the complete filename. Before you can add any data to an untyped file, use the ReWrite command, which defines the filename to use and the number of blocks to add to the file. (Each block of data typically corresponds to one byte of data.) So if you want to define a 5-byte block of data, you’d use the following: ReWrite(myFile, 5);
Storing Varying Size Data in Untyped Files 269 After you define the filename (through the myFile variable name) and the block size, you can start adding actual data by using the BlockWrite command, which specifies the filename to use, the actual data to add, and the number of records to add like this: BlockWrite(myFile, MyData, 1); The preceding command tells the computer to use the file defined by the myFile variable and store the data from the MyData variable as a single record into the file. After you’re done writing data to a file, you need to close the file by using the CloseFile command like this: CloseFile(myFile); Reading data After you store data in an untyped file, you can retrieve it again by first using the Reset command that defines the filename to use and the size of the records you want to retrieve. So if you want to retrieve data from an untyped MyData.dat file, you could assign the variable name myFile to the MyData.dat file, such as Book II Chapter 8 Reading and Saving Files AssignFile(myFile, ‘MyData.dat’); Then you could use the Reset command to tell the computer to open the file defined by the myFile variable like this: Reset(myFile, 5); The Reset command also defines the size of each block of data to retrieve, typically measured in bytes. So the preceding command defines a block of 5 bytes. Because untyped files contain records of varying sizes, here are two ways to read an untyped file. The first way is to scan the file from beginning to end, like a text file. To do that in Delphi, you can use a WHILE loop. Inside the WHILE loop, you can put a BlockRead command that defines which file to read data from, a variable to store this data, and how many bytes to retrieve at a time like this: while not Eof(myFile) do begin BlockRead(myFile, Storage, 1); ShowMessage(IntToStr(Storage)); end; The WHILE loop tells the computer to keep looping as long as the computer hasn’t reached the end of the file (Eof) identified by the myFile variable.