Scandir function Explanation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.