怎样使用C语言列出某个目录下的文件?

如题所述

用C语言列出目录下的文件,在linux下可采用readdir()函数来实现,代码实现过程为:

    打开目录

    循环读目录,输出目录下文件

    关闭目录指针

参考代码:

#include <dirent.h>
#include <stdio.h>
int main()
{
    DIR *dirp; 
    struct dirent *dp;
    dirp = opendir("."); //打开目录指针
    while ((dp = readdir(dirp)) != NULL) { //通过目录指针读目录
        printf("%s\n", dp->d_name );
    }      
    (void) closedir(dirp); //关闭目录
    return 0;
}

在windows下,代码如下:

#include <io.h>
#include <stdio.h>

void printDir( const char* path )
{
    struct _finddata_t data;
    long hnd = _findfirst( path, &data );    // 查找文件名与正则表达式chRE的匹配第一个文件
    if ( hnd < 0 )
    {
        perror( path );
    }
    int  nRet = (hnd <0 ) ? -1 : 1;
    while ( nRet >= 0 )
    {
        if ( data.attrib == _A_SUBDIR )  // 如果是目录
            printf("   [%s]*\n", data.name );
        else
            printf("   [%s]\n", data.name );
        nRet = _findnext( hnd, &data );
    }
    _findclose( hnd );     // 关闭当前句柄
}
void main()
{
printDir("d:/*.*");
}

相关函数说明:

long _findfirst( char *filespec, struct _finddata_t *fileinfo );  
// 功  能 : 提供与filespec指定入口泛式匹配的第一个文件.通常后继用_findnext()函数来完成某泛式下的文件遍历.  
// 头文件 : #include <io.h>  
// 参  数 : filespec - 目标文件规范,可以包含通配符  
//          fileinfo - 文件信息buffer  
// 返回值 : 成功返回唯一的搜索句柄  
//          出错返回-1,且设置errno为如下值:  
//          ENOENT 该泛式无法匹配  
//          EINVAL 无效文件名  
int _findnext( long handle, struct _finddata_t *fileinfo );  
// 功  能 : 按照前面_findfirst中的泛式规则,查找下一个符合该泛式的文件,并以此为依据修改fileinfo中的值  
// 头文件 : #include <io.h>  
// 参  数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)  
//          fileinfo    - 文件信息buffer  
// 返回值 : 成功返回0  
//          出错返回-1
int _findclose( long handle );  
// 功  能 : 关闭搜寻句柄并释放相应资源  
// 头文件 : #include <io.h>  
// 参  数 : long handle - 搜索句柄(通常由紧靠其前的_findfirst()返回)  
// 返回值 : 成功返回0  
//          出错返回-1

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-03
C语言本身没有提供象dir_list()这样的函数来列出某个目录下所有的文件。不过,利用C语言的几个目录函数,你可以自己编写一个dir_list()函数。首先,头文件dos.h定义了一个find_t结构,它可以描述DOS下的文件信息,包括文件名、时间、日期、大小和属性。其次,C编译程序库中有_dos_findfirst()和_dos_findnext()这样两个函数,利用它们可以找到某个目录下符合查找要求的第一个或下一个文件。dos_findfirst()函数有三个参数,第一个参数指明要查找的文件名,例如你可以用“*.*”指明要查找某个目录下的所有文件。第二个参数指明要查找的文件属性,例如你可以指明只查找隐含文件或子目录。第三个参数是指向一个find_t变量的指针,查找到的文件的有关信息将存放到该变量中。dos_findnext()函数在相应的目录中继续查找由_dos_findfirst()函数的第一个参数指明的文件。_dos_findnext()函数只有一个参数,它同样是指向一个find_t变量的指针,查找到刚文件的有关信息同样将存放到该变量中。利用上述两个函数和find_t结构,你就可以遍历磁盘上的某个目录,并列出该目录下所有的文件,请看下例:#include <stdio.h>#include <direct.h>#include <dos.h>#include <malloc.h>#include <memory.h>#include <string.h>typedef struct find_t FILE_BLOCKvoid main(void);void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * /int ret_code; / * Define a variable to store the return codes * // * Use the "*.*" file mask and the 0xFF attribute mask to listall files in the directory, including system files, hiddenfiles, and subdirectory names. * /ret_code = _dos_findfirst(" *. * ", 0xFF, &f_block);/* The _dos_findfirst() function returns a 0 when it is successfuland has found a valid filename in the directory. * /while (ret_code == 0){/* Print the file's name * /printf(" %-12s\n, f_block, name);/ * Use the -dos_findnext() function to look本回答被提问者采纳
相似回答