So, I was doing an exercise that prevedes to open a directory and save his files to use them for other functions. The code is:
int main()
{
printf("%sApro la cartella salvataggi%s\n", COL_YELLOW, COL_GRAY);
int i = 0;
int initsaves = 10;
char **saveFiles = (char **)malloc(initsaves * sizeof(char *));
int dim = initsaves - 1;
DIR *saveDir;
errno = 0;
if ((saveDir = opendir(pathSaves)) == NULL)
{
if (errno == ENOENT)
mkdir(pathSaves, S_IRWXU | S_IRWXG | S_IRWXO);
else
{
printf("%i\n", errno);
errExit("Error in opening savefile");
}
}
struct dirent *controlSaves = readdir(saveDir);
do
{
if (i == dim - 1)
{
dim *= 2;
saveFiles = (char **)realloc(saveFiles, dim);
}
if (strncmp(controlSaves->d_name, ".", 1) != 0)
{
saveFiles[i] = controlSaves->d_name;
printf("%s\n", saveFiles[i]);
i++;
}
controlSaves = readdir(saveDir);
} while (controlSaves != NULL);
free(saveFiles);
if (closedir(saveDir) == -1)
{
errExit("Problema nel chiudere la cartella");
}
printf("%sFINITO%s\n", COL_RED, COL_GRAY);
return 0;
}
Other information, pathSave
is defined as "/Salvataggi". In WSL it works.
Now, while I was doing the exercise, the terminal gave me a munmap chunk error. I've done some text, but only deleting the closedir() I could eliminate the error. Now, I don't want let open the directory after the end of the program, and I want to know why should be the closedir to cause a pointer error.
Also, I use Visual Studio Code. I have windows, so I use WSL for the system calls.
PS: Sorry for my english