返回C中文件夹中的文件列表
我有以下代码,它将在控制台上打印给定文件夹中具有给定扩展名的所有文件:返回C中文件夹中的文件列表,c,pointers,mingw,dirent.h,C,Pointers,Mingw,Dirent.h,我有以下代码,它将在控制台上打印给定文件夹中具有给定扩展名的所有文件: int scandir(char dirname[], char const *ext) /* Scans a directory and retrieves all files of given extension */ { DIR *d = NULL; struct dirent *dir = NULL; d = opendir(dirname); if (d) {
int scandir(char dirname[], char const *ext)
/* Scans a directory and retrieves all files of given extension */
{
DIR *d = NULL;
struct dirent *dir = NULL;
d = opendir(dirname);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (has_extension(dir->d_name, ext))
{
printf("%s\n", dir->d_name);
}
}
closedir(d);
}
return(0);
}
这个函数可以工作,但我想修改它,使其返回一个文件名数组。
(我做了很多谷歌搜索,但只提供了像我这样的功能,可以打印到控制台)
我对C和“低级”编程相当陌生,所以我不确定如何正确处理这里的内存。
当我不知道角色数组将有多大时,如何创建并向其添加内容
我正在使用MinGW..您可以使用
realloc
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
extern char *strdup(const char *src);
int scandir(char ***list, char dirname[], char const *ext)
/* Scans a directory and retrieves all files of given extension */
{
DIR *d = NULL;
struct dirent *dir = NULL;
size_t n = 0;
d = opendir(dirname);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (has_extension(dir->d_name, ext))
{
*list = realloc(*list, sizeof(**list) * (n + 1));
(*list)[n++] = strdup(dir->d_name);
}
}
closedir(d);
}
return n;
}
int main(void)
{
char **list = NULL;
size_t i, n = scandir(&list, "/your/path", "jpg");
for (i = 0; i < n; i++) {
printf("%s\n", list[i]);
free(list[i]);
}
free(list);
return 0;
}
最后,您真的需要阵列吗?考虑使用回叫功能:
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void cb_scandir(const char *src)
{
/* do whatever you want with the passed dir */
printf("%s\n", src);
}
int scandir(char dirname[], char const *ext, void (*callback)(const char *))
/* Scans a directory and retrieves all files of given extension */
{
DIR *d = NULL;
struct dirent *dir = NULL;
size_t n = 0;
d = opendir(dirname);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (has_extension(dir->d_name, ext))
{
callback(dir->d_name);
n++;
}
}
closedir(d);
}
return n;
}
int main(void)
{
scandir("/your/path", "jpg", cb_scandir);
return 0;
}
#包括
#包括
#包括
无效cb_scandir(常量字符*src)
{
/*对已传递的目录执行任何操作*/
printf(“%s\n”,src);
}
int scandir(char dirname[],char const*ext,void(*回调)(const char*))
/*扫描目录并检索给定扩展名的所有文件*/
{
DIR*d=NULL;
struct dirent*dir=NULL;
尺寸n=0;
d=opendir(dirname);
如果(d)
{
而((dir=readdir(d))!=NULL)
{
if(具有扩展名(dir->d_name,ext))
{
回调(dir->d_name);
n++;
}
}
closedir(d);
}
返回n;
}
内部主(空)
{
scandir(“/your/path”,“jpg”,cb_scandir);
返回0;
}
只需谷歌搜索c数组和字符串即可。谢谢。有很多东西要学。特别是指向数组的指针。@AlterMann I将返回以null结尾的字符**+1字符***总是比使用空格**和导致ub更好。
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void cb_scandir(const char *src)
{
/* do whatever you want with the passed dir */
printf("%s\n", src);
}
int scandir(char dirname[], char const *ext, void (*callback)(const char *))
/* Scans a directory and retrieves all files of given extension */
{
DIR *d = NULL;
struct dirent *dir = NULL;
size_t n = 0;
d = opendir(dirname);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (has_extension(dir->d_name, ext))
{
callback(dir->d_name);
n++;
}
}
closedir(d);
}
return n;
}
int main(void)
{
scandir("/your/path", "jpg", cb_scandir);
return 0;
}