User`s manual

Dynamic C Users Manual digi.com 143
The first two statements:
#define FAT_BLOCK
#use "fat.lib"
cause the FAT library to be used in blocking mode.
FAT version 2.01 introduces a configuration library that chooses initialization settings based on the board
type.The statement #use “fat.lib” brings in this configuration library, which in turn brings in the
appropriate device driver library. The following table lists the device drivers that are available in the differ-
ent FAT versions.
Defining the macro _DRIVER_CUSTOM notifies fat_config.lib that a custom driver or hardware
configuration is being used. For more information on how this works, see Section 10.2.5
Next some static variables are declared: a file structure to be used as a handle to the file that will be created
and a buffer that will be used for reading and writing the file.
Now we are in main(). First there are some variable declarations: the integer rc is for the code returned
by the FAT API functions. This code should always be checked, and must be checked if the non-blocking
mode of the FAT is used. The descriptions for each function list possible return codes.
The variable prealloc stores the number of bytes to reserve on the device for use by a specific file.
These clusters are attached to the file and are not available for use by any other files. This has some advan-
tages and disadvantages. The obvious disadvantage is that it uses up space on the device. Some advantages
are that having space reserved means that a log file, for instance, could have a portion of the drive set aside
for its use only. Another advantage is that if you are transferring a known amount of information to a file,
pre-allocation not only sets aside the space so you know you will not get half way through and run out, but
it also makes the writing process a little faster as the allocation of clusters has already been dealt with so
there is no need to spend time getting another cluster.
This feature should be used with care as pre-allocated clusters do not show up on directory listings until
data is actually written to them, even though they have locked up space on the device. The only way to get
unused pre-allocated clusters back is to delete the file to which they are attached, or use the
fat_truncate() or fat_split() functions to trim or split the file. In the case of fat_split(),
the pre-allocated space is not freed, but rather attached to the new file created in the split.
Table 11.
FAT Version Device Driver
1.02, 2.01 sflash_fat.lib
2.05
sflash_fat.lib
nflash_fat.lib
2.13
sflash_fat.lib
nflash_fat.lib
SD_fat.lib