Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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());
   return;
}

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

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

   int rret = unlink(fileName);
   if(rret)
   {
      closedir(dp);
      WARNING("Fail to remove file "<< fileName <<" , errno=" << ERRNO());
      return;
   }

}
closedir(dp);

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
2  
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
add comment

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

Your Answer

 
discard

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.