Originally Posted by
itCbitC
Comparing the filename pfileList->d_name returned from readdir() to "." and ".." is okay if those are the only directories there. But if there are other directories whose names are not known in advance, an alternative is to stat() the filename and see if it is a regular file (S_IFREG) or not. This way all directories can be excluded from the list.
There shouldn't be any other directories inside, but I suppose in the interest of robustness, that sounds like the better way of doing it. Anyway using the following code didn't work:-
Code:
pfileList = readdir( pDIR );
while ( pfileList != NULL )
{
if(pfileList->d_name =="." || pfileList->d_name=="..") //checks to see if the current file read in pfileList is not "." or ".."
{
pfileList =readdir(pDIR);
}
else
{
char *str1=commandName;
char *str2=pfileList->d_name;
printf("\n\nfilename name is %s",str2);
pfileList = readdir( pDIR );
}
}
It still prints out "." and "..", this is just the related section, I have all the variables declared.