23,120
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#define MAX 89
int selectfile(const struct dirent *drt)
{
if(strstr(drt->d_name, ".c") != NULL)
return 1;
else
return 0;
}
main()
{
struct dirent **namelist;
int i, total;
char buff[MAX][13]={0};
total = scandir("./scandir",&namelist, selectfile, alphasort);
printf("total = %d\n", total);
if (total < 0)
{
printf("scandir error: %d\r\n", total);
return 0;
}
for (i = 0; i < total; i++)
strncpy(buff[i],namelist[i]->d_name,12);
// buff[13] = '\0';
for(i = 0; i < total; i++)
printf("%d %s\n", i, buff[i]);
//printf("%d*%s\n",i ,namelist[i]->d_name);
}