我一直在阅读 Brian Kernighan 和 Dennis Ritchie - The C Programming Language,第 8.6 章是关于 UNIX 操作系统下的目录列表的。他们说一切甚至目录都是一个文件。这意味着我应该能够将目录作为文件打开?我已经使用 stdio 函数尝试过它,但没有用。现在,我正在尝试使用 UNIX 系统函数。当然,我没有使用 UNIX,我使用的是 Ubuntu linux。这是我的代码:
#include <syscall.h>
#include <fcntl.h>
int main(int argn, char* argv[]) {
int fd;
if (argn!=1) fd=open(argv[1],O_RDONLY,0);
else fd=open(".",O_RDONLY,0);
if (fd==-1) return -1;
char buf[1024];
int n;
while ((n=read(fd,buf,1024))>0)
write(1,buf,n);
close (fd);
return 0;
}
即使 argn 为 1(无参数)并且我正在尝试读取当前目录,这也不会写入任何内容。 任何想法/解释? :)
最佳答案
关于c - Linux/将目录作为文件打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21405048/