在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. ubuntu 安装 theano

    参考博客: http://www.cnblogs.com/anyview/p/5025704.html 1. 安装gfortran, numpy, scipy, sklearn, blas, atla ...

  2. JSP九大内置对象简介

    1.out对象,是JspWriter类的实例,是向客户端输出内容常用的对象 方法: void println() 向客户端打印字符串 void clear() 清除缓冲区的内容,如果在flush之后调 ...

  3. mongodb的yum源配置和安装

    安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...

  4. mongdb 拓展的下载地址和编译安装(php)

    下载地址:https://pecl.php.net/package/mongodb 编译安装: $ tar zxvf mongodb-mongodb-php-driver-<commit_id& ...

  5. myeclipse 插件下载方式

    myeclipse10,大家都知道,MyEclipse 中有一个烦人的 Software and Workspace center,这东西,加载特别慢,我用10版本基本是没有可能看到这个界面.更别说在 ...

  6. [UOJ422]小Z的礼物

    设要取的物品集合为$S$,$E=n(m-1)+(n-1)m$,$x_T$为覆盖了$T$中至少一个元素的$1\times2$数量 $$\begin{aligned}\sum\limits_{i=1}^\ ...

  7. [POI2015]Odwiedziny

    [POI2015]Odwiedziny 题目大意: 一棵\(n(n\le5\times10^4)\)个点的树,\(n\)次询问从一个点到另一个点的路径上,每次跳\(k\)个点,所经过的点权和. 思路: ...

  8. UVALive 6257 Chemist's vows

    #include<iostream> #include<string.h> #include<stdio.h> #include<ctype.h> #i ...

  9. nginx 注册为linux系统服务

    #! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve ...

  10. Oracle连接步骤

    JDBC实现数据所有的操作: 数据库连接需要的步骤 1.数据库的驱动程序:oracle.jdbc.driver.OracleDriver; 2.连接地址:jdbc:oracle:thin:@主机地址: ...