I want to recursively browse the directory and find all the text files. For this I use this code

while ((entry = readdir(d)) != NULL) { stat(entry->d_name, &st); if (S_ISREG(st.st_mode)) { printf("%20s\t%d\n", entry->d_name, st.st_size); } if (S_ISDIR(st.st_mode)) { DIR* dir; dir = opendir(entry->d_name); cout << "dir" << entry->d_name << endl; cout << "st "<< st.st_mode << endl; read_dir(dir); } } 

but for some reason, when browsing a directory on each file and folder, if (S_ISDIR(st.st_mode)) ,
and if (S_ISREG(st.st_mode)) always false .

  • one
    And did the stat execution succeed? Maybe it is not executed at all, and then you look at the same garbage in st.st_mode . - AnT
  • one
    Probably stat () with an error is returned (== - 1, perror ()), for example due to the fact that not the full name is passed or the relative name is not from that directory. You should also miss the ".", ".." names. - jfs
  • There, the code is not checked for errors anywhere else - 0andriy
  • checked, stat () is executed without error ... ( - Ivan
  • What about other comments (exclude "." , ".." add error checking to all functions)? Give a complete example of the code that shows the error. The minimum reproducible example is jfs

1 answer 1

There are at least three errors in your code:

1) There is no such function in the above text: "read_dir (dir)". There is a "readdir (dir)".

2) You incorrectly form the file name of the current level - there is no top part of the file path. And when processing items that are subdirectories, this error is repeated twice!

3) Well, when analyzing a subdirectory, you are doing something that is not very clear to me - apparently, read_dir (dir) is your function, which should depict repetitive processing? Well, then write it recursively!

I did not bother with the complete alteration of your code to the recursive version, and on the first two points, the code should be like this:

 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/stat.h> #include <dirent.h> int main(int argc, char *argv[]) { DIR *d; struct dirent *entry; struct stat st; char dir_name[256]; char wrk[256]; int rc; if (argc > 1) { strcpy(dir_name, argv[1]); } else { strcpy(dir_name, "."); } d = opendir(dir_name); if (d == NULL) { printf("Ошибка ΠΏΡ€ΠΈ ΠΎΡ‚ΠΊΡ€Ρ‹Ρ‚ΠΈΠΈ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ %s\n", dir_name); exit(-1); } while ((entry = readdir(d)) != NULL) { strcpy(wrk, dir_name); strcat(wrk, "/"); strcat(wrk,entry->d_name); rc = stat(wrk, &st); if (rc != 0 ) { printf("Ошибка ΠΏΡ€ΠΈ Π²Ρ‹Π·ΠΎΠ²Π΅ stat('%s')\n", wrk); exit(-1); } if (S_ISREG(st.st_mode)) { printf("%20s\t%ld\n", entry->d_name, st.st_size); } if (S_ISDIR(st.st_mode)) { DIR* dir; dir = opendir(entry->d_name); printf("dir %s\n", entry->d_name); printf("st %x\n", st.st_mode); readdir(dir); } } } 
  • And forgot to write - ALWAYS organize a special processing of the elements "." and "..". - Sergey
  • thank you, figured out) everything works)) - Ivan