Sign up ×

The case is as this, I create many files, and after the file generation, I use stat to get the all files info under the directory.

The purpose is to purge all the files which are old than the specific days.

But I got a ERROR after the file generation.

The code is like this:

//generate files
DIR *dp;
struct dirent *dirp;
struct stat buf;
time_t now;
const char* fileName;

now = time(NULL);
if((dp = opendir(outputDir.c_str())) == NULL)
   WARNING("Fail to open directory: " << outputDir<< ", errno=" << ERRNO());

while(dirp = readdir(dp))
   fileName = dirp->d_name;
   if(!strcmp(fileName, ".") || !strcmp(fileName, "..")) continue;
   int ret = stat(fileName, &buf);
      WARNING("Fail to get info for files "<< fileName <<" via stat, errno=" << ERRNO());

   if(now - buf.st_mtime <= days * 24 * 3600) continue;

   int rret = unlink(fileName);
      WARNING("Fail to remove file "<< fileName <<" , errno=" << ERRNO());


I got a ERROR during this line:

int ret = stat(fileName, &buf);

The errno is 2, No such file or directory. But I checked file, it does exit, and I flush the stream after the file writing.

Any one knows the cause? Thanks.

share|improve this question
I'm guessing you will get a quicker response over on Stack Overflow. –  Steven D Sep 21 '10 at 4:20
Yes, thanks for your editing. –  Kuyo Sep 21 '10 at 5:32

1 Answer 1

up vote 4 down vote accepted

I guess you just forgot to strcat-prepend the outputDir before your fileName.

share|improve this answer
Yes, that is root cause. Thanks very much! –  Kuyo Sep 21 '10 at 5:30

Your Answer


