
Write a File
112
SWRU455A – February 2017 – Revised March 2017
Copyright © 2017, Texas Instruments Incorporated
File System
3. Close the file, function sl_FsClose().
The next subsections provide detailed descriptions of the functions involved in the write file process.
8.4.2 Create a File versus Open for Write
The host provides different functions for creating a new file, or open for write an existing file. The following
is a list of open file methods.
1. Open-Create-Write: By default, if the file does not exist, the device creates a new file and opens it for
write; otherwise the device opens the file for write.
Example:
_i32
FileHdl;
unsigned char
DeviceFileName[180];
_u32
MaxSize, MasterToken;
FileHdl =
sl_FsOpen(
unsigned char
*)DeviceFileName,
SL_FS_CREATE|SL_FS_CREATE_SECURE |
SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),
&MasterToken);
if
(FileHdl < 0 )
{
/*error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}
2. Open-Create: Creates a new file; the open function returns an error if a file with the same name
already exists.
Example:
_i32
FileHdl;
unsigned char
DeviceFileName[180];
_u32
MaxSize, MasterToken;
FileHdl =
sl_FsOpen(
unsigned char
*)DeviceFileName,
SL_FS_CREATE|SL_FS_CREATE_SECURE |
SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),
&MasterToken);
if
(FileHdl < 0 )
{
/*error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}
3. Open-Write: Opens an existing file for write; the open function returns an error if the file does not exist.
Example:
_i32
FileHdl;
unsigned char
DeviceFileName[180];
_u32
MaxSize, MasterToken;
FileHdl =
sl_FsOpen(
unsigned char
*)DeviceFileName,
SL_FS_OVERWRITE | SL_FS_CREATE_SECURE |
SL_FS_CREATE_NOSIGNATURE | SL_FS_CREATE_MAX_SIZE( MaxSize ),
&MasterToken);
if
(FileHdl < 0 )
{
/* error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}