a. Open File

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

a. Open File

The FOpen() command is used to open a file.

 

 

Command Prototype

 

uint16_t FOpen(const char* String, int16_t Mode);

 

Parameters

Description

String

File name

Mode

Different modes to control file opening behavior. Refer to the table given below for various mode values and its description.

 

Modes

Description

FILE_READ

Refers to the read access to the object. Data can be read from the file

FILE_OPEN_EXISTING

Opens the file. If the file does not exist, then this command fails

FILE_WRITE

Refers to the write access to the object. Data can be written to the file

FILE_CREATE_NEW

Creates a new file. If the file already exists then, this command fails

FILE_CREATE_ALWAYS

Creates a new file. If the file already exists, then it will be truncated and overwritten

FILE_OPEN_ALWAYS

It will open an existing file. If the file does not exist, then a new file will be created.

 

 

Code

 

 

void setup()

{

 char STR1[] = "ABCDEFG";

 int16_t actual;

 

/* Initialize CleO - needs to be done only once */

 CleO.begin();

 

/* Start building a screen frame */

 CleO.Start();

 

 CleO.StringExt(FONT_MEDIUM, 160, 100, LIGHT_GREEN, TM, 0, 0,

 "Creating test.txt...");

 

 /* Open file for write */

int16_t f = CleO.FOpen("@test.txt", FILE_CREATE_ALWAYS | FILE_WRITE);

 

 CleO.StringExt(FONT_MEDIUM, 160, 240, LIGHT_GREEN, TM, 0, 0,

 "Writing ABCDEFG...");

/* Write into file */

 CleO.FWrite(f, strlen(STR1) + 1, (uint8_t*)STR1, actual);

 

 /* Print number of bytes written */

 char buf[30];

 sprintf(buf, "%d bytes written", actual);

 CleO.StringExt(FONT_MEDIUM, 160, 380, LIGHT_GREEN, TM, 0, 0, buf);

 

/* Close file using file handle */

 CleO.FClose(f);

 

/* Display completed screen frame */

 CleO.Show();

}

 

void loop() { }

 

 

Description

 

In the above code snippet, the code highlighted in yellow color shows how the file open operation is performed using the FOpen() command, by creating a test.txt file in SD card in the write mode. Once all transactions are completed, the file is closed using the FClose() command.

 

Use the @ symbol only when performing the file operation from the eFlash and not from the SD Card.