c. Write File - FWrite()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

c. Write File - FWrite()

The FWrite() command is used to write data to a file.

 

 

Command Prototype

 

uint16_t FWrite(int16_t Handle, int16_t Bytestowrite, uint8_t* Buffer, int16_t &Byteswritten);

 

Parameters

Description

Handle

The value returned from FOpen()

Buffer

Location to store the read data

bytestowrite

Indicates how many bytes to be written

byteswritten

Actual number of bytes written

 

 

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, 400, 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, 400, 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, 400, 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 write operation is performed using the FWrite() command. It checks how many bytes were written. Once all transactions are completed, 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.

 

 

Output

 

Here is the output displayed upon executing the code snippet -

 

FILE_WRITE