d. Read File - FRead()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

d. Read File - FRead()

The FRead() command is used to read data from the file.

 

 

Command Prototype

 

uint16_t FRead(int16_t Handle, uint8_t* Buffer, int16_t Bytestoread, int16_t & Bytesread);

 

Parameters

Description

Handle

The value returned from FOpen()

Buffer

Location to store the read data

Bytestoread

Indicates how many bytes to be read

Bytesread

Actual number of bytes read

 

 

Code

 

 

void setup() {

 char STR1[] = "ABCDEFG";

 int16_t actual;

 char buf[30];

 uint32_t u32;

 

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

 CleO.begin();

 

 /* Start building a screen frame */

 CleO.Start();

 

 CleO.StringExt(FONT_MEDIUM, 400, 50, LIGHT_GREEN, TM, 0, 0,

 "Opening ball.png...");

 /* Open ball.png file in read mode */

 int16_t g = CleO.FOpen("@Pictures/ball.png", FILE_READ);

 

/* Request for file size */

 CleO.FSize(g, u32);

 sprintf(buf, "File size:%d bytes", u32);

 CleO.StringExt(FONT_MEDIUM, 400, 100, LIGHT_GREEN, TM, 0, 0, buf);

 

/* Request for file pointer location */

 CleO.FTell(g, u32);

 sprintf(buf, "File pointer at:%d", u32);

 CleO.StringExt(FONT_MEDIUM, 400, 150, LIGHT_GREEN, TM, 0, 0, buf);

 CleO.StringExt(FONT_MEDIUM, 400, 200, LIGHT_GREEN, TM, 0, 0,

 "Reading file...");

 uint8_t buf1[5];  char buf2[5];

 

/* Perform file read */

CleO.FRead(g, buf1, 4, actual);

sprintf(buf2, "%x %x %x %x", buf1[0], buf1[1], buf1[2], buf1[3]);

CleO.StringExt(FONT_MEDIUM, 400, 250, LIGHT_GREEN, TM, 0, 0, (char *) buf2);

 

/* Print number of actual bytes read */

 sprintf(buf, "Bytes read:%d", actual);

 CleO.StringExt(FONT_MEDIUM, 400, 300, LIGHT_GREEN, TM, 0, 0, buf);

 

/* Request for file pointer location */

 CleO.FTell(g, u32);

 sprintf(buf, "File pointer at:%d", u32);

 CleO.StringExt(FONT_MEDIUM, 400, 350, LIGHT_GREEN, TM, 0, 0, buf);

 

/* Check for end of file */

 if (CleO.FEOF(g))

   CleO.StringExt(FONT_MEDIUM, 400, 400, LIGHT_GREEN, TM, 0, 0, "EOF reached");

 

/* Close file using file handle */

 CleO.FClose(g);

 CleO.StringExt(FONT_MEDIUM, 400, 450, LIGHT_GREEN, TM, 0, 0, "File closed");

 

/* Display completed screen frame */

 CleO.Show();

}

 

void loop() { }

 

 

Description

 

In the above code snippet, the code highlighted in yellow color shows how the file read operation is performed using the FRead() command and how the file pointer location can be requested. It also checks how many bytes were read and whether the end of file is reached. Once all operations 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.

 

 

 

 

Output

 

Here is the output displayed upon executing the code snippet -

 

FILE_READ