I am trying to save the names and sizes of files in a directory in this way:
For the first file everything is fine. On the second file it says it can't determine the file path. This is because at the end I clear the "mainPath", but then when the reassignment takes place the variable "path" is also cleared.
if ((directory = opendir(path)) != NULL)
{
while((d = readdir(directory)) != NULL)
{
if(strcmp(d->d_name, "..")!=0 && strcmp(d->d_name, ".")!=0)
{
strcpy(fileSpec[i].fileName, d->d_name);
char *mainPath = path;
strcat(mainPath, d->d_name);
fileSpec[i].fileSize = stat_filesize(mainPath);
printf("\t%s ---> %ld\n", fileSpec[i].fileName, fileSpec[i].fileSize);
i++;
strcpy(mainPath, "");
}
}
closedir(directory);
}
There's something I don't understand. Why is it that when I use strcpy(mainPath, "");
only the variable "mainPath" is not cleaned?
This is because I need to pass the path as an argument.