zoukankan      html  css  js  c++  java
  • 简单目录操作,几个重要的结构体

    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include<error.h>
    #include<errno.h>
    #include<unistd.h>
    #include<strings.h>
    #include<stdbool.h>

    #include<sys/stat.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<fcntl.h>
    #include<dirent.h>
    int main(int argc,char **argv)
    {
    if(argc != 2)
    {
    printf("Usage: %s <dir>",argv[0]);
    exit(0);
    }
    struct stat fileinfo; //注意st_mode st_size 以及设备号等。
    bzero(&fileinfo,sizeof(fileinfo));
    stat(argv[1],&fileinfo);//获取文件信息,放到fileinfo中
    if(!S_ISDIR(fileinfo.st_mode))//判断是否为一个目录
    {
    perror("is not dir");
    exit(0);
    }

    DIR *dp = opendir(argv[1]);
    printf("current dir is:%s",getcwd(NULL,0));//getcwd(NULL,0)获取当前路径,参数固定
    chdir(argv[1]);//进入目录参数要填对。
    printf("current dir is:%s",getcwd(NULL,0));
    struct dirent *ep;  //目录结构体,注意:d_name,d_ino

    while(1)
    {
    ep = readdir(dp);//读取目录操作,返回目录中文件的指针,读取完指针自动向下移。
    if(ep == NULL)
    {
    break;
    }
    bzero(&fileinfo,sizeof(fileinfo));
    stat(ep->d_name,&fileinfo);
    printf("[%u] [%s] [%u] ",ep->d_ino,ep->d_name,fileinfo.st_size);

    }
    closedir(dp);

    }

    ----------------------------------

    struct dirent

    {

    long d_ino; /* inode number 索引节点号 */

    off_t d_off; /* offset to this dirent 在目录文件中的偏移 */

    unsigned short d_reclen; /* length of this d_name 文件名长 */

    unsigned char d_type; /* the type of d_name 文件类型 *

    char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */}

  • 相关阅读:
    SpringMVC_04重定向和转发
    Spring事务声明
    SpringMVC_02_RequestMapping
    SpringMVC_01_初识
    Spring通过mybatis操作数据库
    SpringMVC_03_RestFul
    SpringMVC_05_json数据传输
    退役
    2017ICPC青岛 J.Suffix
    版本不兼容问题(未找到程序集)
  • 原文地址:https://www.cnblogs.com/defen/p/5191512.html
Copyright © 2011-2022 走看看