scandir.c 474 B

1234567891011121314151617181920212223
  1. #include <dirent.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. int filter(const struct dirent* dirent) {
  6. return strstr(dirent->d_name, "3") == NULL;
  7. }
  8. int main(void) {
  9. struct dirent** array;
  10. int len = scandir("example_dir/", &array, filter, alphasort);
  11. if (len < 0) {
  12. perror("scandir");
  13. return -1;
  14. }
  15. for(int i = 0; i < len; i += 1) {
  16. puts(array[i]->d_name);
  17. free(array[i]);
  18. }
  19. free(array);
  20. }