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.
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);
}
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.
#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>
usingnamespace std;
shortint 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.