- 1、本文档共18页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Linux下的目录操作函数
文件及目录函数(37)
chdir, chmod, chown, chroot
fchdir, fchmod, fchown, ftruncate
lchown, truncate,
get_current_dir_name, getcwd, getwd
opendir, closedir, readir, scandir, seekdir, rewinddir, telldir
(mkdir, rmdir)
fstat, lstat, stat
ftw, nftw
link, readlink, symlink, unlink
access, alphasort, realpath, remove, rename, umask
utime, utimes
access: 判断是否有存取文件的权限
头文件: unistd.h
函数定义: int access(const char *pathname, int mode);
说明: access()会检查是否可以读/写某一已存在的文件. 参数 mode有几种情况组合, R_OK,
W_OK, X_OK和 F_OK. R_OK, W_OK 与 X_OK用来检查文件是否具有读取, 写入和执行的权
限. F_OK则是用来判断该文件是否存在. 由于 access()只作权限的核查, 并不理会文件形态或
文件内容, 因此, 如果一目录表示为可写入, 表示可以在该目录中建立新文件等操作, 而非意
味此目录可以被当做文件处理. 若所有欲查核的权限都通过了检查则返回 0值, 表示成功, 只要
有一权限被禁止则返回-1.
错误代码:
EACCESS 参数 pathname所指定的文件不符合所要求测试的权限
EROFS 欲测试写入权限的文件存在于只读文件系统内
EFAULT 参数 pathname 指针超出可存取内存空间
EINVAL 参数 mode不正确
ENAMETOOLONG 参数 pathname太长
ENOTDIR 参数 pathname 为一目录
ENOMEM 核心内存不足
ELOOP 参数 pathname 有过多符号连接问题
EIO I/O 存取错误
附加说明: 使用 access()作用户认证方面的判断要特别小心, 例如在 access()后再做 open()的空
文件可能会造成系统安全上的问题.
应用举例:
#include stdio.h
#include unistd.h
int main(void)
{
if(access(/etc/passwd, R_OK) == 0)
{
printf(/etc/passwd can be read\n);
}
return 0;
}
运行结果:
/etc/passwd can be read
alphasort: 依字母顺序排序目录结构
头文件: dirent.h
定义函数: int alphasort(const struct dirent **a, const struct dirent **b);
说明: alphasort()为 scandir()最后调用的函数, 详细说明请参考 scandir().
应用举例:
/* 读取/目录下的目录结构, 并依字母顺寻排列 */
#include stdio.h
#include dirent.h
int main(void)
{
struct dirent **namelist;
int i, total;
total = scandir(/, namelist, 0, alphasort);
if(total 0)
perror(scandir);
else
{
for(i = 0; i total; i++)
{
printf(%s\n, namelist[i]-d_name);
}
printf(total = %d\n, total);
}
return 0;
}
运行结果:
.
..
.autofsck
bin
boot
...
total = 24
chdir: 改变当前的工作目录
头文件: unistd.h
函数定义: int chdir(const char *path);
说明: chdir()用来将当前的工作目录该变成为参数 path所指的目录. 若成功返回 0, 失败返回-1,
errno为错误代码.
您可能关注的文档
- In planta side-chain glucosinolate modification in Arabidopsis.pdf
- In-sight and RobotComms_ABB_serial.pdf
- ILL_Laue2012_Schwarzenbach.pdf
- Incidence of cardiovascular disease in individuals with psoriasis.pdf
- Incidence+and+prevention+of+venous+thromboembolism+in+acutely+ill+hospitalized+elderly+Chinese.pdf
- Inclusive D^{+-} Production in p p-bar Collisions with Massive Charm Quarks.pdf
- Increasing efficiency in the supply chain for short shelf life goods using RFID tagging.pdf
- India Custom Tariff chap-72(iron and steel).pdf
- Indirect methods for wake potential integration.pdf
- inet-d71-rev01-8189.pdf
最近下载
- 2024年延安职业技术学院高职单招职业技能测验2018-2023年典型考题含答案解析.docx
- 2023年上海第二工业大学网络工程专业《计算机组成原理》科目期末试卷A(有答案).docx VIP
- 2022浙ST19壁挂式轻便消防水龙及室内消火栓安装.pdf
- 《热力学第二定律》练习题及解答.pdf
- 浅析科隆公司成本管理存在的问题及对策.docx VIP
- 小学数学“图形与几何”结构化教学.pptx VIP
- 2025年苏州工业园区服务外包职业学院单招职业适应性测试题库精选.docx VIP
- 企业成本管理存在的问题及对策.docx VIP
- 2025年幼儿园教师职称五套试题及答案 .pdf VIP
- 2024中国营养健康食品行业蓝皮书-CIC灼识咨询.ppt
文档评论(0)