opendir,readdir, closedir, stat函数的使用

opendir();------------------------------------------------------------------

readdir();-------------------------------------------------------------------

读取成功是返回结构体类型指针

文件编号(索引号):ino

closedir();-----------------------------------------------------------

---------------------------------------------------------------------------------------------------

ls功能,实现目录名显示

stat();------------------------------------------------

man 2 stat

要准备一个放文件属性的结构体,并取其地址传入

对应00007  (用最后三位来代表others)

判断模式,有两种方法:(1)mode与单个种类相&,不为0即为真 (2)mode与全1(即7)相&,判断是否等于想要的模式

判断wxr,文件使用权限,单个相&,判断是否为0(法一)

法二:判断文件类型,与全1相&,判断是否相等

以下是实现上述要求的示例 C 语言代码: ``` #include <stdio.h> #include <string.h> #include <unistd.h> #include <pwd.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <fcntl.h> // 定义一个结构体来存储文件信息 typedef struct FileInfo { char name[256]; // 文件名 long address; // 物理地址 int protectionCode; // 保护码 int length; // 文件长度 } FileInfo; // 定义一个函数来验证用户的密码是否正确 int checkPassword(const char* username, const char* password) { struct passwd* pwd = getpwnam(username); if (pwd == NULL) { return 0; // 用户名不存在 } char* encryptedPassword = crypt(password, pwd->pw_passwd); return strcmp(encryptedPassword, pwd->pw_passwd) == 0; } // 定义一个函数来遍历目录中的文件 void listDirectory(const char* path) { DIR* dir = opendir(path); if (dir == NULL) { perror("opendir"); return; } struct dirent* entry; while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); } closedir(dir); } // 定义一个函数来创建新文件 void createFile(const char* path) { int fd = creat(path, 0644); if (fd == -1) { perror("creat"); return; } close(fd); } // 定义一个函数来删除文件 void deleteFile(const char* path) { if (unlink(path) == -1) { perror("unlink"); } } // 定义一个函数来打开文件 int openFile(const char* path) { int fd = open(path, O_RDONLY); if (fd == -1) { perror("open"); } return fd; } // 定义一个函数来关闭文件 void closeFile(int fd)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值