Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.