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);





The value returned from FOpen()


Indicates how many bytes to be written


Location to store the read data


Actual number of bytes written






void setup()


 char STR1[] = "ABCDEFG";

 int16_t actual;


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



 /* Start building a screen frame */



 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 */



/* Display completed screen frame */




void loop() { }





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.





Here is the output displayed upon executing the code snippet -