在C/C++编程时,需要获取目录下面的文件列表信息。

1.数据结构

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字符 */
}
 
struct __dirstream
  {
    void *__fd;                        /* `struct hurd_fd' pointer for descriptor.  */
    char *__data;                /* Directory block.  */
    int __entry_data;                /* Entry number `__data' corresponds to.  */
    char *__ptr;                /* Current pointer into the block.  */
    int __entry_ptr;                /* Entry number `__ptr' corresponds to.  */
    size_t __allocation;        /* Space allocated for the block.  */
    size_t __size;                /* Total valid data in the block.  */
    __libc_lock_define (, __lock) /* Mutex lock for this structure.  */
  };

typedef struct __dirstream DIR;

 
 
2.程序示例
其中程序中win不支持文件类型(d_type),可以根据文件名称后缀来判断文件类型;linux可以直接使用d_type判断是目录还是文件。
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h> int main(){
DIR *dir;
struct dirent *ptr;
dir = opendir("."); ///open the dir while((ptr = readdir(dir)) != NULL) ///read the list of this dir
{
#ifdef _WIN32
printf("d_name: %s\n", ptr->d_name);
#endif
#ifdef __linux
printf("d_type:%d d_name: %s\n", ptr->d_type,ptr->d_name);
#endif
}
closedir(dir);
return ;
}

程序输出:

C/C++ 获取目录下的文件列表信息的更多相关文章

  1. PHP 批量获取指定目录下的文件列表(递归,穿透所有子目录)

    //调用 $dir = '/Users/xxx/www'; $exceptFolders = array('view','test'); $exceptFiles = array('BaseContr ...

  2. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  3. 取CPU序列号,获取网卡,取硬盘系列号,获取目录下的文件,强制删除目录

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. Python开发【笔记】:获取目录下所有文件

    获取文件 import os def sub_dirs(rdir): li = os.listdir(rdir) return li def main(rdir): content = sub_dir ...

  5. 阿里云OSS 获取目录下所有文件

    public class AliyunHandle { public static string accessKeyId = "a1uI5xxxxxxxxxrP4H"; publi ...

  6. Golang获取目录下的文件及目录信息

    一.获取当前目录下的文件或目录信息(不包含多级子目录) func main() {  pwd,_ := os.Getwd() //获取当前目录  //获取文件或目录相关信息  fileInfoList ...

  7. shell获取目录下所有文件夹的名称并输出

    获取指定目录/usr/下所有文件夹的名称并输出: shell代码: #!/bin/bash #方法一 dir=$(ls -l /usr/ |awk '/^d/ {print $NF}') for i ...

  8. python递归获取目录下指定文件

    获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...

  9. File类 递归 获取目录下所有文件文件夹

    package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...

随机推荐

  1. Hibernate or JPA Annotation中BLOB、CLOB注解写法

    BLOB和CLOB都是大字段类型,BLOB是按二进制字节码来存储的,而CLOB是可以直接存储字符串的. 在hibernate or JPA Annotation中,实体BLOB.CLOB类型的注解与普 ...

  2. iOS Sprite Kit教程之xcode安装以及苹果帐号绑定

    iOS Sprite Kit教程之xcode安装以及苹果帐号绑定 其它的网站上下载安装Xcode 有时候,应用商店下载较慢,所以用户也可以选择从其他网站下载Xcode安装文件.下面讲解这种Xcode的 ...

  3. ceph部署过程中的错误

    ceph版本-jewel 用ssd盘来journal ,格式分区权限问题 [ceph-node2][WARNIN] ceph_disk.main.FilesystemTypeError: Cannot ...

  4. Django框架(一)-Django初识

    Django初识 一.Web框架本质—自己实现Web框架 1.所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端 import socket sk = sock ...

  5. int类型的整数转换成汉字

    int类型的整数转换成汉字 一.源代码:IntegerNumberToChinese.java package cn.com.zfc.example; import java.util.Scanner ...

  6. Loj #6560 小奇取石子

    题面 分类讨论一波,n小的暴力2^n,n大的背包. #include<bits/stdc++.h> #define ll long long using namespace std; co ...

  7. GNU C __attribute__ 机制简介

    摘要: 在学习linux内核代码及一些开源软件的源码(如:DirectFB),经常可以看到有关__attribute__的相关使用.本文结合自己的学习经历,较为详细的介绍了__attribute__相 ...

  8. Codeforces Round #294 (Div. 2)A - A and B and Chess 水题

    A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. mysql慢查询配置

    1.慢查询有什么用? 能记录下所有执行超过long_query_time时间的SQL语句, 帮你找到执行慢的SQL, 方便我们对这些SQL进行优化. 2. 如何开启慢查询? 首先我们先查看MYSQL服 ...

  10. Pyhton 编程风格

    博文概要:1.Python语句和语法2.Python标识符3.基本编程风格 一.Python语句和语法 1.可以从一行的任何地方开始2.续行 \: ''':闭合操作符,单一语句跨多行 3.代码组 .缩 ...