I made a new project that has to open a folder using DIR *
DIR * directory=openDir(...); // openDir is a function returning DIR * after it was opened
than to make a new folder in it, so far so good
mkdir(path,permissions);
and now i have to run through the folder entries using
while ((ent=readdir(directory))){ do stuff ; }
but it wont get into the loop.

and if i add this code AFTER the previous code it works and i get into the loop

DIR *dirr=openDir(path);// same openDir function from above
while(ent=readdir(dirr)){stuff}

i do get into the while loop.

any ideas?

edit: (another question)
and what can i do in order for the readdir to avoid subdirectories? ( avoiding the new folder i just created in it)

Dani AI

Generated

Most likely the directory stream has already been consumed before your second pass. Reopening worked because opendir returns a fresh stream at the start of the directory. Either reset the stream with rewinddir() or close and re-open the directory before you call readdir() again. readdir() returns NULL at end-of-directory or on error — check errno to tell which (see the readdir man page).

A robust pattern:

DIR *ds = opendir(dirpath);
if (!ds) { perror("opendir"); return; }

/* reset if you previously walked this stream */
rewinddir(ds);   /* or closedir() + opendir() */

errno = 0;
struct dirent *ent;
while ((ent = readdir(ds)) != NULL) {
    if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
        continue;
    if (ent->d_type == DT_DIR)
        continue;   /* skip subdirs; if DT_UNKNOWN, call stat() on the full path */
    /* handle regular file */
}
if (errno)
    perror("readdir");
closedir(ds);

To avoid subdirectories: check d_type first (fast), but some filesystems return DT_UNKNOWN; in that case use stat() and test S_ISREG() / S_ISDIR() on the entry's full pathname (you noted using S_ISREG — that’s the right approach when you stat()).

One more caution: modifying a directory while iterating can produce unspecified results on some systems. If you create the subdirectory before scanning, prefer reopening the directory (or rewinddir) so you get a consistent stream. See the POSIX/OpenGroup notes and the readdir/rewinddir man pages for details: readdir(3) man page and rewinddir(3) man page.

This explains why saw NULL until reopening, and supports ’s advice to test entries for directories.

Recommended Answers

All 3 Replies

Here is a code snippet I wrote some time ago, maybe it will help you with your question. As for the sub-directory question, see the example in my code snippet -- you have to test for them.

The code snippet was written in c++, so just ignore the code you may not understand and concentrate on the loops.

well it is easier for me to understand c++ because that's what we are programming in usually.
eventualy I just used the macros S_ISREG for just handling the regular files , so done with that.

but I don't understand why I have to open the DIR* twice.
I do it once to check if the folder exists
and I add a sub folder . <---------------------------- could this be the problem? I dont think so...
so I want to continue using it but DIRENT wont get a thing but NULL.
then I reopen it and it works fine

well i sent the exercise already but still if you know whats wrong I'd be happy for an answer
thanks

You need to post the code you wrote if you expect anyone to answer your questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.