Is there a way to list all subdirectories in a given directory path in C? I was hoping I would be able to do it with the stat()
function but it only works on files.
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- C#: How do i get 2 lists into one 2-tuple list in
As others have noted,
stat(2)
works fine on files and devices of all types. It reads through symbolic links to the file at the far end; if you need the information about the symbolic link itself, uselstat(2)
.To list the names of all directories within a single directory (non-recursively), use a combination of the
readdir(3)
family of functions.To list the names of all directories recursively, use the
ftw(3)
ornftw(3)
functions to do a 'file tree walk' (from whence cometh their names; 'n' is for 'new').stat works on directories too.
You want readdir(3).