Linux C language traversal folder-general Linux technology-Linux programming and kernel information, the following is a detailed description. I learned how to traverse folders in C language in LINUX.
Several members in struct dirent:
D_type: 4 indicates a directory, and 8 indicates a file.
D_reclen: 16 indicates a subdirectory or file, and 24 indicates a non-subdirectory.
D_name: directory or file name
The Code is as follows for your reference only.
# Include
# Include
# Include
Void List (char * path)
{
Struct dirent * ent = NULL;
DIR * pDir;
PDir = opendir (path );
While (NULL! = (Ent = readdir (pDir )))
{
If (ent-> d_reclen = 24)
{
If (ent-> d_type = 8)
Printf ("% s \ n", ent-> d_name );
Else
{
Printf ("subdirectory: % s \ n", ent-> d_name );
List (ent-> d_name );
Printf ("Return % s \ n", ent-> d_name );
}
}
}
}
Int main (int argc, char * argv [])
{
List (argv [1]);
Return 0;
}
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