小编Lov*_*ode的帖子

使用c中的scandir()以升序打印目录

使用scandir()它时,用于alphasort以相反的顺序对目录内容列表进行排序.现在如何scandir()在c中使用升序打印目录.

.并且..必须在最顶层.

这是代码:


#include<stdio.h>
#include <dirent.h>
#include<string.h>
#include<sys/dir.h>
#include<malloc.h>
int main(void)
{
   struct dirent **namelist;
   int n;

   n = scandir(".", &namelist,NULL,alphasort);
   if (n < 0)
      perror("scandir");
   else {
      while (n--) {
         printf("%s\n", namelist[n]->d_name);
         free(namelist[n]);
      }
      free(namelist);
   }
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

4
推荐指数
1
解决办法
3479
查看次数

标签 统计

c ×1