已经四天都没有更新博客了,今天小编复习下文件操作相关函数的使用。大家可以来这里和小编一起复习哦哦。
一、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 file
write(fd,"hello\n",6);
// 设置文件指针到文件首部
lseek(fd,0,SEEK_SET);
// read file
char 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