User`s manual

Dynamic C Users Manual digi.com 159
10.2.4.2.4 Creating Files and Subdirectories
While the fat_Open() function is versatile enough to not only open a file but also create a file or a sub-
directory, there are API functions specific to the tasks of creating files and subdirectories.
The fat_CreateDir() function is used to create a subdirectory one level at a time.
The first parameter, my_part, points to a partition structure. This pointer must point to a mounted parti-
tion. Some of the sample programs, like fat_create.c, declare a local pointer and then search for a
partition pointer in the global array fat_part_mounted[]. Other sample programs, like
fat_shell.c, define an integer to be used as an index into fat_part_mounted[]. Both methods
accomplish the same goal of gaining access to a partition pointer.
The second parameter contains the directory or subdirectory name relative to the root directory. If you are
creating a subdirectory, the parent directory must already exist.
Once DIR1 is created as the parent directory, a subdirectory may be created, and so on.
Note that a forward slash is used in the pathname instead of a backslash. Either convention may be used.
The backslash is used by default. To use a forward slash instead, define FAT_USE_FORWARDSLASH in
your application or in FAT.LIB.
A file can be created using the fat_CreateFile() function. All directories in the path must already
exist.
The first parameter, my_part, points to the static partition structure set up by fat_AutoMount().
The second parameter contains the file name, including the directories (if applicable) relative to the root
directory. All paths in the FAT library are specified relative to the root directory.
The third parameter indicates the initial number of bytes to pre-allocate. At least one cluster will be allo-
cated. If there is not enough space beyond the first cluster for the requested allocation amount, the file will
be allocated with whatever space is available on the partition, but no error code will be returned. If no clus-
ters can be allocated, the -ENOSPC error code will return. Use NULL to indicate that no bytes need to be
allocated for the file at this time. Remember that pre-allocating more than the minimum number of bytes
necessary for storage will reduce the available space on the device.
The final parameter, &my_file, is a file handle that points to an available file structure. If NULL is
entered, the file will be closed after it is created.
rc = fat_CreateDir(my_part, "DIR1");
rc = fat_CreateDir(my_part, "DIR1/SUBDIR");
rc = fat_CreateFile(my_part, "DIR1/SUBDIR/FILE.TXT", &prealloc,
&my_file);