C语言怎么读取某一文件夹下的所有文件夹和文件?

C语言怎么读取某一文件夹下的所有文件夹和文件?,第1张

读桥埋困取的代码方式如下:

intmain()

{

longfile

struct_finddata_tfind

_chdir("d:\\")

if((file=_findfirst("*.*",&find))==-1L)

{

printf("空白!\n")

exit(0)

}

printf("%s\n",find.name)

while(_findnext(file,&find)==0)

{

printf("%s\n",find.name)

}

_findclose(file)

return0

}

用C语言读取目录中的文件名的方法:

1、如果是在window环境下,可以用一敏念下方法:

使用stdlib.h头文件声明的system()函数,调用液祥系统命令dir,把c:目录下文件列表写入文件dir.txt中

2、使用dirent.h头文件中声明的opendir(),readdir()函数;

3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数:

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_BLOCK

void

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

list

all

files

in

the

directory,

including

system

files,

hidden

files,

and

subdirectory

names.

*

/

ret_code

=

_dos_findfirst("槐举

*.

*

",

0xFF,

&f_block)

/*

The

_dos_findfirst()

function

returns

a

0

when

it

is

successful

and

has

found

a

valid

filename

in

the

directory.

*

/

while

(ret_code

==

0){/*

Print

the

file's

name

*

/

printf("

%-12s

,

f_block,

name)

/

*

Use

the

-dos_findnext()

function

to

look

Function SearchFiles(Path As String, FileType As String)

Dim Files() As String '文件路径

Dim Folder() As String '文件夹路径

Dim a, b, c As Long

Dim sPath As String

sPath = Dir(Path &FileType) '查找第一个文件

Do While Len(sPath) '循环到没有文件为止

a = a + 1

ReDim Preserve Files(1 To a)

Files(a) = Path &sPath '将文件目录和文件名组合,并存放到数组中御枣

List1.AddItem Files(a) '加入list控件中

sPath = Dir '查找下一个文件

DoEvents '让出控制权

Loop

sPath = Dir(Path &"\镇弊拆", vbDirectory) '查找第一个文件夹

Do While Len(sPath) '循环到没有文件夹为止

If Left(sPath, 1) <>"." Then '为了防止重复查找

If GetAttr(Path &"\" &sPath) And vbDirectory Then '如果是文件夹则。。。。。。

b = b + 1

ReDim Preserve Folder(1 To b)

Folder(b) = Path &sPath &"\" '将目录和文件夹名称组合形成新的目录,并存放到数组中

End If

End If

sPath = Dir '查找下一个文件夹

DoEvents '让出控制权

Loop

For c = 1 To b '使用递归方法,遍历所有目录

SearchFiles Folder(c), FileType

Next

End Function

Private Sub Command1_Click() '调卜碧用

SearchFiles "e:\", "*.exe"

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12148863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存