Dirent.h problem (the program crashes while trying to read the directory)

i want to make a program that gets all the folders and subfolerds from the D: directory ,but the program stops at around 32 000 files read and i dont know why ,it stops in the ent = readdir (dir)) != NULL structure. I am the owner and all files have full access. Also ,the path is always correct ,but i really don't know why it stops in that strucutre tho.. Please help :)
I put the n to be the limit 100 000 because it will make it easier ,also ,i have 172 000 files ,so the problem isn't from the infinite structure.


```
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <io.h>
#include <Shellapi.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <dirent.h>
using namespace std;

short int t[100003], n=1;
struct {char numefisiere[100];}v[200000];
#define limitafis 100000
void listFile(){
int i=0, it=0;
v[0].numefisiere[0]='D';
v[0].numefisiere[1]=':';
v[0].numefisiere[2]='/';
char path[500]="D:/";

//the while loop i have problems with :(
DIR *dir;
struct dirent *ent;
while (n<limitafis){
dir = opendir (path);
cout<<path<<'\n';
while ((ent = readdir (dir)) != NULL){
//here it crashes :(
cout<<ent->d_name<<'\n';
strcpy(v[n++].numefisiere, ent->d_name);
if (v[n-1].numefisiere[0]=='.' && (v[n-1].numefisiere[1]=='.' || v[n-1].numefisiere[1]==NULL))
--n;
else {t[++it]=i;
}
}


//this is the non-crashy part ,just the making of path ,but is correct
++i;
strcpy(path,"D:");
int adr[10], nadr=0, e=1;
adr[nadr]=i;
while (t[adr[nadr]])
adr[++nadr]=t[adr[nadr-1]];
while (nadr>=0){
path[++e]='/';
for (int z=0; v[adr[nadr]].numefisiere[z]; ++z){
path[++e]=v[adr[nadr]].numefisiere[z];
}
--nadr;
}
for (int z=e+1; path[z]; ++z)
path[z]=NULL;
}
i_fis_adaug=n;
closedir(dir);
}

int main (){
listFile();
}
```
Last edited on
Use code tags when posting code.

It looks like your closedir is in the wrong place so you're opening but not closing all the dirs. It should be in the outer while loop somewhere, presumably right after the first inner while.

Another prime example of why indentation MATTERS!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <io.h>
#include <Shellapi.h>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <dirent.h>
using namespace std;

short int t[100003], n = 1;

struct {
  char numefisiere[100];
} v[200000];

#define limitafis 100000
void listFile()
{
  int i = 0, it = 0;
  v[0].numefisiere[0] = 'D';
  v[0].numefisiere[1] = ':';
  v[0].numefisiere[2] = '/';
  char path[500] = "D:/";

//the while loop i have problems with :(
  DIR *dir;
  struct dirent *ent;
  while (n < limitafis) {
    dir = opendir(path);
    cout << path << '\n';
    while ((ent = readdir(dir)) != NULL) {  //here it crashes :(
      cout << ent->d_name << '\n';
      strcpy(v[n++].numefisiere, ent->d_name);
      if (v[n - 1].numefisiere[0] == '.' && (v[n - 1].numefisiere[1] == '.' || v[n - 1].numefisiere[1] == NULL))
        --n;
      else {
        t[++it] = i;
      }
    }

    //this is the non-crashy part ,just the making of path ,but is correct
    ++i;
    strcpy(path, "D:");
    int adr[10], nadr = 0, e = 1;
    adr[nadr] = i;
    while (t[adr[nadr]])
      adr[++nadr] = t[adr[nadr - 1]];
    while (nadr >= 0) {
      path[++e] = '/';
      for (int z = 0; v[adr[nadr]].numefisiere[z]; ++z) {
        path[++e] = v[adr[nadr]].numefisiere[z];
      }
      --nadr;
    }
    for (int z = e + 1; path[z]; ++z)
      path[z] = NULL;
  }
  i_fis_adaug = n;
  closedir(dir);  // YES!!! you call opendir() many times and never closedir()
}

int main()
{
  listFile();
}


Other problems

1. The alphabet soup of t,v,n etc.
Aside from loop variables, everything else should have a meaningful name.

2. More functions.
Your listFile() tries to do too much. There should be at least two functions:
- scan a directory
- figure out what the next directory to scan is.
Shorter functions make it easier to spot when things like opendir/closedir need to be balanced.

3. Over reliance on global variables.


Last edited on
How do you compile this code?
Neither clang nor VS 2019 compiles it.

Why don't you use std::filesystem ?
https://en.cppreference.com/w/cpp/filesystem
dirent.h is not a standard include file. It's posix and not provided for Windows/VS
Maybe it's cygwin?
Looks to be something for Linux only:
https://www.systutorials.com/docs/linux/man/0p-dirent.h/

The OP might be better off learning how to use <filesystem>, as thmm suggested earlier.
Topic archived. No new replies allowed.