导航:首页 > 文件类型 > c目录所有文件名

c目录所有文件名

发布时间:2025-08-25 05:27:13

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

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\n, f_block, name); / * Use the -dos_findnext() function to look

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

读取的代码方式如下:

intmain()

{

longfile;

struct_finddata_tfind;

_chdir("d:\");

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

{

printf("空白! ");

exit(0);

}

printf("%s ",find.name);

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

{

printf("%s ",find.name);

}

_findclose(file);

return0;

}

⑶ c++获取指定目录下的文件名

可以使用searchenv函数来实现,参考代码如下:

intmain(void)
{
charpathbuffer[_MAX_PATH];
charsearchfile[]="×.EXE";
charenvvar[]="PATH";

//:
_searchenv(searchfile,envvar,pathbuffer);//C4996
//Note:_searchenvisdeprecated;considerusing_searchenv_s
if(*pathbuffer!='')
printf("Pathfor%s: %s ",searchfile,pathbuffer);
else
printf("%snotfound ",searchfile);
}

⑷ C语言:如何得到指定地址的文件夹中所有文件的文件名和其修改时间 包括子文件内的

//获取指定目录下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;

//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
while ((ent = readdir(pDir)) != NULL)
{ //统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//关闭目录
closedir(pDir);
// myLog("共%d个文件\n", count);

//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}

//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d个文件:%s\n", i, ent->d_name);
i++;
}
} //for
//关闭目录
closedir(pDir);

*fileCount = count;
return fileNameList;
}

阅读全文

与c目录所有文件名相关的资料

热点内容
如何切换oracle数据库 浏览:959
陌陌升级有什么好处 浏览:390
oracle数据库管理从入门到精通pdf 浏览:648
mysql数据库培训 浏览:441
whasapp聊天记录在哪个文件夹 浏览:108
30岁去哪里学编程 浏览:30
word为尾部空格添加下划线 浏览:190
大众朗逸的数据总线在哪里 浏览:500
广西大专编程哪个好 浏览:99
qq头像戴口罩男生动漫 浏览:440
远程桌面连接系统找不到指定文件 浏览:416
qq男生背影头像伤感带字 浏览:997
爱思助手读设备开机密码 浏览:315
js动态生成word 浏览:646
危险化学品按什么分类建立数据库 浏览:300
手机号重新注册微信怎么恢复数据 浏览:815
全脑和编程哪个好 浏览:709
教师学习编程语言有什么用 浏览:444
extjs创建对象 浏览:608
苹果6听筒声音小破音 浏览:945

友情链接