Problem with gSystem->GetDirEntry()?

Hello,

I have some code that should tell me if a file is a regular file or a directory
but for any reason this does not always return a correct answer.

#include <TROOT.h>
#include <TSystem.h>

#include <sys/stat.h>

int main(int argc, char **argv)
{
void dirp = gSystem->OpenDirectory("/home/broessli/Documents/flatcone/fit");
const char
afile;
struct stat st_buf;
int status;

while ((afile = gSystem->GetDirEntry(dirp)))
{
status = stat(afile,&st_buf);
if (S_ISREG (st_buf.st_mode)) {
printf ("%s is a regular file.\n", afile);
}
if (S_ISDIR (st_buf.st_mode))
{
printf ("%s is a directory.\n", afile);
}
}
gSystem->FreeDirectory(dirp);
return 0;

}

e.g. on my directory

vv is a directory. OK
fgui.C~ is a directory. NO
basic2.C is a directory. NO
str.cc is a directory. NO
t~ is a directory. NO

while running the code independently from Root works:

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
int status;
struct stat st_buf;

if (argc != 2) {
    printf ("Usage: progName <fileSpec>\n");
    printf ("       where <fileSpec> is the file to check.\n");
    return 1;
}

status = stat (argv[1], &st_buf);
if (status != 0) {
    printf ("Error, errno = %d\n", errno);
    return 1;
}

if (S_ISREG (st_buf.st_mode)) {
    printf ("%s is a regular file.\n", argv[1]);
}
if (S_ISDIR (st_buf.st_mode)) {
    printf ("%s is a directory.\n", argv[1]);
}

return 0;

}

e.g.

/home/broessli/Documents/flatcone/fit/t~ is a regular file.

Thank You in advance for help,

Bertrand Roessli

PS: I have Root5.18

It seems that adding

gSystem->ChangeDirectory(PathToDir);

at the beginning f the code solves the problem

:smiley:

Bertrand Roessli

Hi,

yes, because you are only providing stat() with the file name. You need to add the directory name or stat() will not find out outside ./

Cheers, Axel.