Я очень смущен. В цикле while я добавляю каждое имя файла в массив и затем печатаю его.
Однако в цикле for он печатает некоторые странные вещи и останавливается в середине.
Пожалуйста, помогите мне исправить это.
char *commands[1000];
char *str;
DIR * dir;
struct dirent * entry;
char *env = getenv("PATH");
do {
str = strsep(&env, ":");
if(str != NULL)
if(strlen(newEnv) > 0) {
dir = opendir(str);
if( dir == NULL ) break;
flag = 0;
while((entry = readdir(dir)) != NULL) {
commands[++count] = entry->d_name;
printf("---%i %s\n", count ,commands[count]); // prints fine
}
closedir(dir); // close directory
}
} while(newEnv);
commands[++count] = '\0';
printf("count = : %i\n", count);
for(int i = 0; i < count; i ++)
{
if(commands[i] == NULL)
break;
printf("aaa%i %s\n\n", i, commands[i]); //problem loop
}