Scandir function Explanation
2009-10-30 10:51
Scandir function: Reading a specific directory data
Table header files: #include <dirent.h>
Define functions: Int scandir (const char *dir, struct dirent **namelist, NT (*select) (const struct dirent *), NT
(*compar) (CONST struct Dirent * *, const struct dirent**));
Function Description:
Scandir () scans the directory file specified by dir, selects the directory structure into the parameter NameList array via the function specified by the parameter select, and finally calls the
The Compar parameter specifies the function to sort the directory data in the NameList array. Each time a directory structure is read from the catalog file, the structure is passed to the parameter select
function, the Select function returns 0 if you do not want to copy this directory structure to the NameList array, or select all directory structures if the Select is a null pointer. Scandir () will
Call Qsort () to sort the data, parameter compar is the parameter of Qsort (), and Alphasort () is used if the directory name letter is to be arranged. Structure Dirent definition Please refer to
Readdir ()
Return value: Success returns the number of data structures copied to the NameList array, or 1 if an error occurs
Error code: ENOMEM Core memory is low
Example
#include <dirent.h>
Main ()
{
struct Dirent **namelist;
int n;
n = Scandir (".", &namelist, 0, Alphasort);
if (n < 0)
Perror ("Scandir");
Else
{
while (n--)
{
printf ("%s\n", namelist[n]->d_name);
Free (namelist[n]);
}
Free (namelist);
}
}
Transferred from: http://blog.sina.com.cn/s/blog_6a113b0a0100uzb3.html
Scandir function Explanation