Searches a file.

Syntax

int findfirst(
  const char *filename,
  struct ffblk *ffblkp
);

Parameters

filename
[in] Specify the file to be searched in the format of "name.extension". You can include the following wildcards:
?Matches one character.
*Matches a series of characters up to the end of the name, or extension.
ffblkp
[out] A pointer to the ffblk structure that stores the search results.

Return value

If there is a file that matches the search conditions, OK is returned, otherwise ERROR is returned.

Remarks

The file with the name specified by filename is searched in the current working directory, and the search result of the first matching file is returned to the ffblk structure specified by ffblkp.
Note that directories that match the name specified in filename will also be returned. You can see the attributes of the file in the attrib member of the ffblk structure.
To find the next matching file after the file returned in the ffblk structure, use the findnext function.

Requirements

Header file:
lib.h
Library file:
libSTARTUPOPH5000.a

Sample

#include <stdio.h>
#include "lib.h"

void makeTestFiles()
{
    FILE *fp;

    f_mkdir("/TESTDIR");
    f_mkdir("/TESTDIR/SUBDIR");

    fp = fopen("/TESTDIR/test1.txt", "w");
    fclose(fp);

    fp = fopen("/TESTDIR/test2.txt", "w");
    fclose(fp);
}

void main(void)
{
    struct ffblk finddata;

    makeTestFiles();

    //Change directory to the searching directory first.
    f_chdir("/TESTDIR");

    if (findfirst("*.*", &finddata) == OK){
        do {
            if (finddata.attrib & AM_DIR)
                printf("SubDir:");
            else
                printf("File  :");

            printf("%s.%s\n", finddata.name, finddata.ext);
        }while (findnext(&finddata) == OK);
    }

    f_chdir("/");

    while(1){
        Idle();
    }

    /*
    <Output>
    SubDir:SUBDIR.
    File  :TEST1.TXT
    File  :TEST2.TXT
    */
}

See also

Last updated: 2020/11/18