Linux文件操作函数(二)

目录

获取文件信息

编写 ls-l 指令Demo

创建一个硬链接

删除一个文件的目录项

编写 mv 指令Demo

创建一个软链接

读取软链接对应的文件名

文件重命名

修改文件权限

测试文件是否拥有某种权限


获取文件信息

—stat函数

            int stat(const char *pathname, struct stat *statbuf);
            int fstat(int fd, struct stat *statbuf);
            int lstat(const char *pathname, struct stat *statbuf);

            pathname—文件名 

            statbuf—文件信息结构体

            struct stat {
                      dev_t     st_dev;         /* ID of device containing file */
                      ino_t     st_ino;         /* Inode number */
                      mode_t    st_mode;        /* File type and mode */
                      nlink_t   st_nlink;       /* Number of hard links */
                      uid_t     st_uid;         /* User ID of owner */
                      gid_t     st_gid;         /* Group ID of owner */
                      dev_t     st_rdev;        /* Device ID (if special file) */
                      off_t     st_size;        /* Total size, in bytes */
                      blksize_t st_blksize;     /* Block size for filesystem I/O */
                      blkcnt_t  st_blocks;      /* Number of 512B blocks allocated *
            };

            成功返回 0 ,失败返回 -1 并设置 errno  

编写 ls-l 指令Demo

#include <stdio.h>      //stand c library
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>                                                                                        
int getFileInfor(const char *Filename); //get file information
/*
 *error handling 
 */
void errorHand(const char *error)
{
        perror(error);  //execute failed
        exit(0);
}

/*
 * directions
 */
int operatDir(const char *pathname)
{
        DIR *dirp;
        char path[256] = {'\0'};
        struct dirent *dirs = NULL;     //directions information 
        dirs = (struct dirent *)malloc(sizeof(struct dirent *)); //
        dirp = opendir(pathname);
        if(dirp == NULL){

                perror("opendir error");        //open dir failed
                return -1;
        }
        while((dirs = readdir(dirp)) != NULL){

                sprintf(path,"%s/%s",pathname,dirs->d_name);
                getFileInfor(path);     //get file information

        }

        closedir(dirp); //close direction
        free(dirs);     //free up space

        return 0;
}

/*
 *get file information
 */
int getFileInfor(const char *Filename)
{
        int re_sta;
        struct stat infor;
        re_sta = stat(Filename,&infor);
        if(re_sta == -1){

                perror("stat error");   //File information cannot be obtainede 
                return -1;
        }else{

                printf("%-20s %-20ld\n",Filename,infor.st_size);    //printf file infor
        }

        return 0;
}

int main(int argc,char **argv)
{

        if(argc == 1){

                operatDir("."); //current direction
        }else

                while(--argc > 0)
                       operatDir(*++argv);
        return 0;
}

创建一个硬链接

—link函数

            int link(const char *oldpath, const char *newpath);

            oldpath—源文件名 

            newpath—新创建的硬链接文件名

            成功 返回 0 ,失败返回 -1 并设置 errno  

删除一个文件的目录项

—unlink函t数

            int unlink(const char *pathname);

            pathname—删除的文件名

            成功 返回 0 ,失败返回 -1 并设置 errno  

编写 mv 指令Demo

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc,char **argv)
{
        int re_lin;
        if(argc != 3){
                printf("you enter the wrong parameter!\n");
                exit(0);
        }
        re_lin = link(argv[1],argv[2]);
        if(re_lin == -1){

                perror("link error");   //remove failed!
                exit(0);
        }
        unlink(argv[1]); //delet old file name

        return 0;
}

创建一个软链接

—symlink函t数

            int symlink(const char *target, const char *linkpath);

            target—软链接的名字

            linkpath—要创建软连接的文件的路径名(推荐使用绝对路径)

            成功 返回 0,失败返回 -1 并设置 errno  

读取软链接对应的文件名

—readlink函t数

            ssize_t readlink(const char *pathname, char *buf, size_t bufsiz);

            pathname—软链接名

            buf—存取读到的软链接对应文件名

            bufsiz—buf的大小

            成功 返回 读取到buf的字节数,失败返回 -1 并设置 errno  

文件重命名

—rename函t数

            int rename(const char *oldpath, const char *newpath);

            oldpath—源文件名

            newpath—新文件名

            成功 返回0,失败返回 -1 并设置 errno

修改文件权限

—chmod函数

            int chmod(const char *pathname, mode_t mode);

            pathname—文件名 

            mode—要修改的文件权限

            成功返回 0 ,失败返回 -1 并设置 errno  

测试文件是否拥有某种权限

—access函数

            int access(const char *pathname, int mode);

            pathname—文件名 

            mode—要检查的文件权限

            检测文件所有权限均成功 返回 0 ,失败返回 -1 并设置 errno  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值