You are currently viewing the documentation for:
ECF_OpenFile opens a file on a mounted file system.
ECF_ErrorCode ECF_OpenFile(
   struct ECF_FileHandle *pFileHandle,
   const char *filename,
   uint8_t flags
This is a pointer to a struct ECF_FileHandle structure to hold data about the open file. The contents of the struct ECF_FileHandle will be cleared. There is no need to initialize it.
This is the name of the file to open. Files are always specified with their full paths including the drive letter. To open a file called "Log.txt" on drive A you will need to specify the filename: A:\Log.txt (which is "A:\\Log.txt" when entered as a C string) To open a file called "My" in the directory "My folder" on drive B you need to specify the filename: B:\My folder\My ("B:\\My folder\\My" as a C string) The maximum total path length including the trailing NUL character is 260 characters.
Specifies in which mode the file should be opened. Select one of ECF_MODE_READ, ECF_MODE_READ_WRITE and ECF_MODE_APPEND.
Opens the file for reading. Reading will start at the beginning of the file.
Opens the file for reading and writing. Reading and writing will start from the beginning of the file. If the file doesn't exist it will be created.
Opens the file for reading and writing. Writing the file will write to the end of it. If the file doesn't exist it will be created.
Return value
Returns one of the EcFAT error codes (ECFERR_SUCCESS on success)
If the call to ECF_OpenFile was successful you can now use your file handle to call other file operations. You may open the same file several times but you may only open it once in write mode. This enables you to have one process that logs data to a file while another process reads it. On a journaled file system writes to the file system structures are protected but normal data writes are not. If you open the file with ECF_MODE_DATAJOURNAL, data writes will be journaled as well. This means that either the write will either be written in full or not at all (in case of a power loss). You may not write more than one sector size of data in each write (typically 512 or 4096 bytes). Since the journal needs to be written on each write, writes to files opened with ECF_MODE_DATAJOURNAL will be slower than normal.
Example Code
#include <EcFAT/EcFAT.h>

void main(int argc, char **argv)
   // ...Mount file system on 'A' here...
   struct ECF_FileHandle fileHandle;
   const char *cszMessage = "This will be written to the file\n";

   if(ECF_OpenFile(&fileHandle, "A:\\Log.txt", ECF_MODE_APPEND)
      // Error checking omitted
      ECF_WriteFile(&fileHandle, cszMessage, strlen(cszMessage));
See also