【Linux】文件操作总结
来源:互联网 发布:盐城市大数据产业园 编辑:程序博客网 时间:2024/05/16 17:34
已经四天都没有更新博客了,今天小编复习下文件操作相关函数的使用。大家可以来这里和小编一起复习哦哦。
一、accsee 函数
1、作用:
测试指定文件是否拥有某种权限。
2、函数原型:
#include <unistd.h> int access(const char *pathname, int mode);
3、参数:
pathname 文件名 mode 权限类别
4、返回值:
若成功,返回0;若失败,返回 -1;
#include<unistd.h>#include<stdio.h>#include<stdlib.h>int main(int argc,char *argv[]){if( argc<2 ){perror("a.out filename");exit(1);}int ret = access(argv[1],W_OK);if( ret == -1 ){perror("access");exit(1);}printf("You can write this file\n");return 0;}
二、chmod 函数
1、作用:
改变文件的权限
2、函数原型:
#include <sys/stat.h> int chmod(const char *path, mode_t mode);
3、参数:
pathname 文件名 mode 权限类别
4、返回值:
若成功,返回0;若失败,返回 -1;
#include<unistd.h>#include<stdio.h>#include<stdlib.h>int main(int argc,char *argv[]){if( argc<2 ){perror("a.out filename");exit(1);}int ret = chmod(argv[1],0777);if( ret == -1 ){perror("chmod");exit(1);}return 0;}
三、truncate 函数
1、作用:
将参数 path 指定的文件对的 大小改变为参数 length 指定的大小。如果原来的文件大小比 参数 length 大,则超过的部分会被删去。
2、函数原型:
#include <unistd.h> #include <sys/types.h> int truncate(const char *path, off_t length);
3、参数:
pathname 文件名 length 指定文件大小
4、返回值:
若成功,返回0;若失败,返回 -1;
#include<unistd.h>#include<stdio.h>#include<stdlib.h>int main(int argc,char *argv[]){if( argc<2 ){perror("a.out filename");exit(1);}int ret = truncate(argv[1],1024);if( ret == -1 ){perror("truncate");exit(1);}return 0;}
四、链接函数( link , sylink , readlink , unlink )
1、link 函数
(a)、作用:
创建一个硬链接
(b)、函数原型:
#include <unistd.h> int link(const char *oldpath, const char *newpath);
(c)、返回值:
若成功,返回0;若失败,返回 -1;
2、sylink 函数
(a)、作用:
创建一个软链接
(b)、函数原型:
#include <unistd.h> int symlink(const char *oldpath, const char *newpath);
(c)、返回值:
若成功,返回0;若失败,返回 -1;
3、readlink 函数(a)、作用:
读软链接对应文件名,不是内容
(b)、函数原型:
#include <unistd.h> ssize_t readlink(const char *path, char *buf, size_t bufsiz);
(c)、返回值:
若成功,返回0;若失败,返回 -1;
#include<unistd.h>#include<stdio.h>#include<stdlib.h>int main(int argc,char *argv[]){if( argc<2 ){perror("a.out filename");exit(1);}char buf[512]={0};int ret = readlink(argv[1],buf,sizeof(buf));if( ret == -1 ){perror("readlink");exit(1);}buf[ret]=0;printf("%s\n",buf);return 0;}
4、ulink 函数
(a)、作用:
删除一个文件的目录项,并减少它的链接数
注:如果是符号链接 ,则删除符号链接
如果是硬链接,则硬链接数减 1
如果该文件硬链接数为 0 ,该文件内容才能被真正删除,但是若又进程打开了该文件,则文件暂时不删除直到所有打开该文件的进程都结束时文件才能被删除。
(b)、函数原型:
#include <unistd.h> int unlink(const char *pathname);
(c)、返回值:
若成功,返回0;若失败,返回 -1;
#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<fcntl.h>#include<stdlib.h>int main(void){int fd = open("timefile",O_CREAT|O_RDWR,0644);if( fd == -1 ){perror("open");exit(1);}int ret =unlink("timefile");if( ret == -1 ){perror("unlink");exit(1);}// write filewrite(fd,"hello\n",6);// 设置文件指针到文件首部lseek(fd,0,SEEK_SET);// read filechar buf[20]={0};read(fd,buf,sizeof(buf));printf("%s\n",buf);return 0;}
五、 rename 函数
1、作用:
文件重命名
2、函数原型:
#include <stdio.h> int rename(const char *oldpath, const char *newpath);
3、返回值:
若成功,返回0;若失败,返回 -1;
六、stat、lstat 函数这两个函数才是文件操作函数的重点,它们可以获取文件的信息等。以上介绍的文件操作函数可以都不会,但是这两个函数必须的学会使用。而且可以使用者两个函数
写出一个简单的 ls - l 的功能。
这一节的话在我之前定的博客已经总结好了,链接在这呢。还不赶快往这里使劲的 戳戳戳................
http://blog.csdn.net/qq_35396127/article/details/78530001
全是干货哦哦。
小结:
今天 Linux的文件操作已总结完,小编下面会继续更新。预知后事,请继续关注小编哦哦。还有就是以上介绍的所有函数,如果您想要获取源代码,可以在我对的
GitHub 上获取哦哦哦:
获取 Code 的 URL:
https://github.com/ARRAYLI/LinuxCode
- Linux 文件操作总结
- linux文件操作总结
- Linux 文件操作总结
- linux文件操作总结
- Linux 文件操作总结
- Linux 文件操作总结
- Linux 文件操作总结
- linux文件操作总结
- Linux文件操作总结
- linux文件操作总结
- Linux 文件操作总结
- Linux 文件操作总结
- 【Linux】文件操作总结
- Linux文件操作学习总结
- Linux文件操作学习总结
- Linux文件操作学习总结
- Linux文件操作学习总结
- Linux文件操作学习总结
- 使用sublime 同步编辑从excel导入数据到SQL Server数据库
- Ubuntu下安装Java及环境配置
- 赋予oracle执行存储过程权限和创建表权限
- 前端程序员遇到 Bug 时的 30 个反应,你是哪一种?
- String in C or C++
- 【Linux】文件操作总结
- Keil uVision4 的下载及破解步骤
- oracle取出多个字段列中的最大值和最小值
- TIOBE 11 月编程语言排行榜,脚本语言怎么了?
- Mybatis动态SQL
- 算法-如何计算时间复杂度?
- 10个深度学习软件的安装指南(附代码)
- 洛谷P1358 扑克牌
- 近期活动盘点:基于雷达图像预测未来降水参赛经验分享、大数据基础设施讲座、药品行业分析及大数据应用思享会(11.22-11.29)