如何在Unix中将目录作为文件读取? [英] How do I read a directory as a file in Unix?

查看:16
本文介绍了如何在Unix中将目录作为文件读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道目录只是 unix 中的一个文件,其中包含 inode 编号和文件名称.我如何看待这个?我不能在目录上使用 cat 或 less,在 vi 中打开它只会显示文件列表...没有 inode 编号.

I understand that a directory is just a file in unix that contains the inode numbers and names of the files within. How do I take a look at this? I can't use cat or less on a directory, and opening it in vi just shows me a listing of the files...no inode numbers.

推荐答案

由于这是一个编程问题(它一个编程问题,不是吗?),您应该查看opendirreaddirclosedir 函数.这些是单一 UNIX 规范的一部分.

Since this is a programming question (it is a programming question, isn't it?), you should check out the opendir, readdir and closedir functions. These are part of the Single UNIX Spec.

#include <sys/types.h>
#include <dirent.h>

DIR *opendir (const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);

dirent.h 文件应该有你需要的结构,至少包含:

The dirent.h file should have the structure you need, containing at least:

char   d_name[]    name of entry
ino_t  d_ino       file serial number

请参阅此处了解readdir 手册页 - 它包含指向其他人的链接.

See here for the readdir manpage - it contains links to the others.

请记住,存储在其目录条目中的文件的信息量是最少的.inode 本身包含您从 stat 函数获得的内容,例如时间、大小、所有者、权限等,以及指向实际文件内容的所有重要指针.

Keep in mind that the amount of information about a file stored in the directory entries for it is minimal. The inode itself contains the stuff you get from the stat function, things like times, size, owner, permissions and so on, along with the all-important pointers to the actual file content.

这篇关于如何在Unix中将目录作为文件读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆