zoukankan      html  css  js  c++  java
  • 文件目录相关函数

    //-rw-rw-r-- 2 zijiao zijiao 16 Feb 24 13:20 hello
    
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <time.h>
    #include <pwd.h>
    #include <grp.h>
    
    int main(int argc, char* argv[]){
        if(argc!=2){
            printf("need filename
    ");
            return -1;
        }
        //stat func to get file information
        struct stat sb;
        stat(argv[1],&sb);
    
        //parse the info st_mode, uid, gid, time 
        //st_mode 
        char stmode[11]={0};
        memset(stmode,'-',10);
        if(S_ISREG(sb.st_mode)) {
            stmode[0]='-';
        }
        if(S_ISDIR(sb.st_mode)){
            stmode[0]='d';
        }
        if(S_ISCHR(sb.st_mode)){
            stmode[0]='c';
        }
        if(S_ISBLK(sb.st_mode)){
            stmode[0]='b';
        }
        if(S_ISFIFO(sb.st_mode)){
            stmode[0]='p';
        }
        if(S_ISLNK(sb.st_mode)){
            stmode[0]='l';
        }
        if(S_ISSOCK(sb.st_mode)){
            stmode[0]='s';
        }
        
        //parse permission
        if(sb.st_mode & S_IRUSR) stmode[1]='r';
        if(sb.st_mode & S_IWUSR) stmode[2]='w';
        if(sb.st_mode & S_IXUSR) stmode[3]='x';
        
        if(sb.st_mode & S_IRGRP) stmode[4]='r';
        if(sb.st_mode & S_IWGRP) stmode[5]='w';
        if(sb.st_mode & S_IXGRP) stmode[6]='x';
        
        if(sb.st_mode & S_IROTH) stmode[7]='r';
        if(sb.st_mode & S_IWOTH) stmode[8]='w';
        if(sb.st_mode & S_IXOTH) stmode[9]='x';
        
        //username, groupname can be obtained by func getpwuid and getgrgid 
        
        //time can be obtained by func localtime
        struct tm *filetm=localtime(&sb.st_atim.tv_sec);
        char timebuf[20];
        sprintf(timebuf, "%dmonth  %dday %02d:%02d", filetm->tm_mon+1, filetm->tm_mday, filetm->tm_hour, filetm->tm_min);
        printf("%s %d %s %s %d %s %s", stmode, sb.st_nlink,getpwuid(sb.st_uid)->pw_name, 
               getgrgid(sb.st_gid)->gr_name, sb.st_size,timebuf ,argv[1]);
        return 0;
    }

    stat 函数实现 ls -l

    若查看软链接的,会追溯到源文件。若只想查看软连接,需 lstat

     

     

     

     

     

    access 函数查看文件权限

    查看最大可以打开的文件数

     3-1023

    truncate 截断文件

     可用于扩展文件, 长度大于源文件就扩展。

     截断文件到4B, 长度小于源文件就截断。

    link 函数

    创建硬连接

     创建软链接

     获取软链接源文件

     unlink删除链接

    注意: unlink("hello") hello这个文件就被删除了

     可以写入,但是文件最后不存在。unlink删除硬连接计数,若到0,则删除文件,如果有进程引用当前文件,先不删除此文件,等到进程退了以后再删。

    chown 函数

    rename 函数

     改文件/目录均可

    getcwd 获得当前工作路径

    chdir 改变工作路径

    只是改变程序进程的工作路径,shell的工作路径并没有变。工作路径是进程独有的。

     

     在bbb目录下写文件temp

    mkdir 函数

    rmdir 函数

    目录必须为空

    需求: 统计目录下普通文件的个数,子目录递归

    shell: find ./ -type f | wc -l

    #include <stdio.h>
    #include <unistd.h>
    #include <dirent.h>
    #include <sys/types.h>
    #include <string.h>
    int count =0;
    int fileCount(char *dir){
        //open dir
        DIR* dirp=opendir(dir);
        if(dirp==NULL){
            perror("opendir err
    ");
            return -1;
        }
        struct dirent* drt=NULL;
        while((drt=readdir(dirp))!=NULL){ // if null, reach the end of dir
            if(drt->d_type==DT_DIR){
                if(strcmp(".",drt->d_name)==0 || strcmp("..",drt->d_name)==0 ) continue;
                char newdirname[256]={0};
                sprintf(newdirname,"%s/%s", dir, drt->d_name);
                fileCount(newdirname);
            }else if(drt->d_type==DT_REG){
                count++;
            }
        }
        closedir(dirp);
        return 0;
    }
    
    int main(int argc, char* argv[])
    {   
        if(argc!=2){
            printf("need dir
    ");
            return -1;
        }
        fileCount(argv[1]);
        printf("total file count: %d
    ", count);
        return 0;
    }

    dup 和 dup2

     需求:执行两次输出HW,一次输出到文件中,一次输出到屏幕上

  • 相关阅读:
    双系统重装windows后,修复grub启动
    【转】GeoIP + PHP 完全指南
    【转】Linux压缩打包命令使用方法
    【转】linux screen 的使用 教程
    【转】tar打包命令详解
    【转】SecureCRT 显示乱码问题(总结)
    【转】Windows Server 2008 配置终端服务
    【转】vsftp 添加用户 简单步骤
    【转】用移动硬盘装WIN7出现NTLDR is missing
    【转】Qt集成到VS2008中 ( Qt4.5.1 + qtvsaddin1.0.0 )
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12365953.html
Copyright © 2011-2022 走看看