n. Check File/Dir. Existence - FStat()

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 17 - CleO File System >

n. Check File/Dir. Existence - FStat()

The FStat() command is used to check the existence of a file/directory.

 

 

Command Prototype

 

int cmd_FStat( const char* Path, const char* FileInfoBuf);

 

 

Parameters

Description

Path

The location of the directory

FileInfoBuf

Buffer to store file/directory information. To check the existence, this should be set to NULL. To get the file/directory info, provide a pointer of structure file_info_t to the command.

 

Code

 

 

void setup() {

 int file;

 char buf[9];

 int bytewritten;

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

 "Create TestFStat.txt...");

 file = CleO.FOpen("@TestFStat.txt", FILE_CREATE_ALWAYS | FILE_WRITE);

 CleO.FClose(file);

 //Confirm if the file exist

  if(CleO.FStat("@TestFStat.txt", NULL) == 1) {

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

    "TestFStat.txt OK");    

  } else {

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

    "TestFStat.txt FAILED");     

 }

 CleO.Show();

}

 

void loop() {}

 

 

Description

 

The above code snippet opens the test.txt file in write mode. The code highlighted in yellow color shows how the FStat() command checks for the existence of a file or directory and displays an appropriate message accordingly. The @ symbol is used 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 -

 

FStat