You are currently viewing the documentation for:
ECF_OpenFile
ECF_OpenFile opens a file on a mounted file system.
ECF_ErrorCode ECF_OpenFile(
   struct ECF_FileHandle *pFileHandle,
   const char *filename,
   uint8_t mode
);
Parameters
pFileHandle
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.
filename
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 file.data" in the directory "My folder" on drive B you need to specify the filename: B:\My folder\My file.data ("B:\\My folder\\My file.data" as a C string) The maximum total path length including the trailing NUL character is 260 characters.
mode
Specifies in which mode the file should be opened. Supported modes are:
ECF_MODE_READ:
Opens the file for reading. Reading will start at the beginning of the file.
ECF_MODE_READ_WRITE:
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.
ECF_MODE_APPEND:
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)
Remarks
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.
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)
      == ECFERR_SUCCESS)
   {
      // Error checking omitted
      ECF_WriteFile(&fileHandle, cszMessage, strlen(cszMessage));
      ECF_CloseFile(&fileHandle);
   }
}
See also