c语言opendir打开文件夹,完整程序

Python07

c语言opendir打开文件夹,完整程序,第1张

#include<stdio.h>

#include <sys/types.h>

#include <dirent.h>

int main()

{

DIR *dfd

dfd = opendir("文件夹名称");

closedir(dfd)

//想读取文件夹,用readdir

return 0

}

读取的代码方式如下:

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()函数: