Hello I want to read from and write to a directory just like reading from and writing to files. I always use the open
, read
, write
and close
functions, which means I use descriptors. But doing this on a directory doesn't work, the open
call works, but read
returns -1 and errno
is EISDIR. Am I forced to use streams to read a directory?
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- How to access the camera from my Windows Phone 8 a
The
read()
andwrite()
system calls cannot be used on directories. Instead, thegetdents()
/getdents64()
system calls are used to read a directory. Directories cannot be directly written at all.Futhermore, glibc does not provide a wrapper for the
getdents()
/getdents64()
system calls - instead it provides the POSIX-conformingreaddir()
function, which is implemented using those system calls. Most programs should usereaddir()
, but it is possible to call the system calls directly usingsyscall()
.I found this code here in Stack Overflow (How can I get the list of files in a directory using C or C++?), that helped me a lot in understanding how it works: