g. Get File Pointers - FTell()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

g. Get File Pointers - FTell()

The FTell() command is used to get the current read/write pointer to a file.

 

 

Command Prototype

 

uint16_t FTell(int16_t Handle, uint32_t &Offset)

 

Parameters

Description

Handle

The value returned from FOpen()

Offset

File pointer location to seek

 

 

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, 160, 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, 160, 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, 160, 150, LIGHT_GREEN, TM, 0, 0, buf);

  CleO.StringExt(FONT_MEDIUM, 160, 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, 160, 250, LIGHT_GREEN, TM, 0, 0, (char *) buf2);

 

/* Print number of actual bytes read */

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

 CleO.StringExt(FONT_MEDIUM, 160, 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, 160, 350, LIGHT_GREEN, TM, 0, 0, buf);

 

/* Check for end of file */

 if (CleO.FEOF(g))

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

 

/* Close file using file handle */

 CleO.FClose(g);

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

 

/* Display completed screen frame */

 CleO.Show();

}

 

void loop() { }

 

 

Description

 

The above code snippet opens the test.txt file in the read mode and performs the file read operation. The code highlighted in yellow color shows how the file pointer location is requested and displayed using the FTell() command. Once the process is 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.