打印当前文件路径下的filename

#include <stdio.h>
#include <dirent.h>

int main()
{

        DIR *dirptr = NULL;
        int i = 1;
        struct dirent *entry;
        if((dirptr = opendir(".")) == NULL)
        {
                printf("opendir failed!");
                return 1;
        }
        else
        {
                while(entry = readdir(dirptr))
                {
                        printf("filename%d=%s\n",i,entry->d_name);
                        i++;
                }
                closedir(dirptr);
        }
        return 0;
}

第二种

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>

int main()
{
        DIR *dir = NULL;
        struct dirent *ptr;
        char childBuf[600];
        static int flag = 0;

        if((dir = opendir("."))== NULL)
        {
                printf("opendir failed!");
                return -1;
        }

        while(ptr = readdir(dir))
        {
                memset(childBuf,0,sizeof(childBuf));
                snprintf(childBuf,sizeof(childBuf),"%s/%s",".",ptr->d_name);
                //printf("filename%d=%s\n",1,ptr->d_name);
                printf("%s\n",childBuf);

        }
        closedir(dir);

        return 0;
}

头文件#include<dirent.h>

DIR结构体:

struct __dirstream
   {
    void *__fd;
    char *__data;
    int __entry_data;
    char *__ptr;
    int __entry_ptr;
    size_t __allocation;
    size_t __size;
    __libc_lock_define (, __lock)
   };
typedef struct __dirstream DIR;

需要使用到DIR结构体的函数有:

DIR *opendir(const char *pathname);

struct dirrent readdir(DIR *dir);

void rewindir(DIR *dir);

int closedir(DIR *dir);

long telldir(DIR *dir);

void seekdir(DIR *dir,long loc);

struct dirent 结构体

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字符 */
}

d_type表示文件的类型:

enum
{
    DT_UNKNOWN = 0,         //未知类型
# define DT_UNKNOWN DT_UNKNOWN
    DT_FIFO = 1,            //管道
# define DT_FIFO DT_FIFO
    DT_CHR = 2,             //字符设备
# define DT_CHR DT_CHR
    DT_DIR = 4,             //目录
# define DT_DIR DT_DIR
    DT_BLK = 6,             //块设备
# define DT_BLK DT_BLK
    DT_REG = 8,             //常规文件
# define DT_REG DT_REG
    DT_LNK = 10,            //符号链接
# define DT_LNK DT_LNK
    DT_SOCK = 12,           //套接字
# define DT_SOCK DT_SOCK
    DT_WHT = 14             //链接
# define DT_WHT DT_WHT
};

判断是不是目录或文件有两个宏可以用
D_ISDIR(d_type)
D_ISREG(d_type)

获取directory的过程

1、首先通过opendir函数打开路径,返回一个指向该路径的结构体:DIR

2、使用readdir函数读取DIR指针,返回存储改路径下的文件信息的结构体dirent

3、调用结构体的参数没获取自己想要的文件信息。

最后升级代码

当程序中有这个判断的时候会报出段错误

修改一下代码

#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>

void readList(const char *pathname)
{
        DIR *dir;
        struct dirent *ptr;
        char basename[64] = {0};

        if((dir = opendir(pathname)) == NULL)
        {
                printf("pathname open error\n");
                exit(1);
        }
        while((ptr = readdir(dir)) != NULL)
        {
                if((strncmp(ptr->d_name,".",1) == 0) || (strncmp(ptr->d_name,"..",2) == 0))
                        continue;
                else if(ptr->d_type == 8)
                {
                        snprintf(basename,sizeof(basename),"%s/%s",pathname,ptr->d_name);
                        printf("%s\n",basename);
                        memset(basename,0,sizeof(basename));
                }
                else if(ptr->d_type == 4)
                        readList(ptr->d_name);
        }
        closedir(dir);

}

void main()
{
        char *pathname = ".";
        readList(pathname);
}

效果:

在添加上char *getcwd(char *buf,size_t size)函数

效果:

 1: directory.c                                                                                                                                                                   ?? buffers 
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>

void readList(const char *pathname)
{
        DIR *dir;
        struct dirent *ptr;
        char basename[64] = {0};

        if((dir = opendir(pathname)) == NULL)
        {
                printf("pathname open error\n");
                exit(1);
        }
        while((ptr = readdir(dir)) != NULL)
        {
                if((strncmp(ptr->d_name,".",1) == 0) || (strncmp(ptr->d_name,"..",2) == 0))
                        continue;
                else if(ptr->d_type == 8)
                {
                        snprintf(basename,sizeof(basename),"%s/%s",pathname,ptr->d_name);
                        printf("%s\n",basename);
                        memset(basename,0,sizeof(basename));
                }
                else if(ptr->d_type == 4)
                        readList(ptr->d_name);
        }
        closedir(dir);

}

void main()
{
        char buf[128];
        char *pathname ;
        pathname = getcwd(buf,128);
        readList(pathname);

注意事项:

在linux上搭载samba服务器usb读取window上文件时,会出现window和linux之间的差异,导致程序异常,linux的文件名是有限制的,但是window没有

#ifndef _LINUX_LIMITS_H
 2 #define _LINUX_LIMITS_H
 3 
 4 #define NR_OPEN            1024
 5 
 6 #define NGROUPS_MAX    65536    /* supplemental group IDs are available */
 7 #define ARG_MAX       131072    /* # bytes of args + environ for exec() */
 8 #define LINK_MAX         127    /* # links a file may have */
 9 #define MAX_CANON        255    /* size of the canonical input queue */
10 #define MAX_INPUT        255    /* size of the type-ahead buffer */
11 #define NAME_MAX         255    /* # 文件名最大字符数 */
12 #define PATH_MAX        4096    /* # 相对路径名最大字符数 */
13 #define PIPE_BUF        4096    /* # bytes in atomic write to a pipe */
14 #define XATTR_NAME_MAX   255    /* # chars in an extended attribute name */
15 #define XATTR_SIZE_MAX 65536    /* size of an extended attribute value (64k) */
16 #define XATTR_LIST_MAX 65536    /* size of extended attribute namelist (64k) */
17 
18 #define RTSIG_MAX      32
19 
20 #endif 

此时判断文件夹时需要添加长度判断条件,超过255长度的文件夹不进行读取,如果读取了会因为linux打不开造成异常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘多拉的面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值