User`s manual

142 digi.com File Systems
The sample program has been broken into two functional parts for the purpose of discussion. The first part
deals with getting the file system up and running. The second part is a description of writing and reading
files.
10.2.2.1 Bringing Up the File System
We will look at the first part of the code as a whole, and then explain some of its details.
File Name: Samples\FileSystem\fat_create.c
#define FAT_BLOCK // use blocking mode
#use "fat.lib" // of FAT library
FATfile my_file; // get file handle
char buf[128]; // 128 byte buffer for read/write of file
int main(){
int i;
int rc; // Check return codes from FAT API
long prealloc; // Used if the file needs to be created.
fat_part *first_part; // Use the first mounted FAT partition.
rc = fat_AutoMount( FDDF_USE_DEFAULT );
first_part = NULL;
for(i=0;i < num_fat_devices * FAT_MAX_PARTITIONS; ++i)
{// Find the first mounted partition
if ((first_part = fat_part_mounted[i]) != NULL) {
break; // Found mounted partition, so use it
}
}
if (first_part == NULL) { // Check if mounted partition was found
rc = (rc < 0) ? rc : -ENOPART; // None found, set rc to a FAT error code
} else{
printf("fat_AutoMount() succeeded with return code %d.\n", rc);
rc = 0; // Found partition; ignore error, if any
}
if (rc < 0){ // negative values indicate error
if (rc == -EUNFORMAT)
printf("Device not Formatted, Please run Fmt_Device.c\n");
else
printf("fat_AutoMount() failed with return code %d.\n", rc);
exit(1);
} // OK, file system exists and is ready to access. Let's create a file.