j. Read String - FGetS()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

j. Read String - FGetS()

The FGetS() command is used for reading a string from a file.

 

 

Command Prototype

 

uint16_t FGetS(int16_t Handle, uint8_t* Buffer, int16_t Buflen)

 

Parameters

Description

Handle

The value returned from FOpen()

Buffer

Location to store the read data

Buflen

Length of the buffer in bytes

 

Code

 

 

void setup() {

 char STR1[] = "HelloWorld";

 

/* 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 TestGetS.txt...");

/* open file for write */

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

 

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

                "Writing HelloWorld...");

               

 /* write one string into file */

 CleO.FPutS(f, STR1);

/* close file using file handle */

 CleO.FClose(f);

 

  /* Open to read back */

  char buf[30] = {0};

  int16_t actual;

  f = CleO.FOpen("@TestGetS.txt", FILE_READ);

  CleO.FGetS(f, (uint8_t *)buf, 11);

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

                 "Read back: ");

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

 

 /* close file using file handle */

 CleO.FClose(f);

 

 /* Display completed screen frame */

 CleO.Show();

}

 

void loop() {}

 

 

Description

 

The above code snippet opens the test.txt file in read mode. The code highlighted in yellow color shows how the FGetS() command reads the string from the file and displays it accordingly. 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.

 

 

Output

 

Here is the output displayed upon executing the code snippet -

 

FGetS