3

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.

1
  • 2
    I'm guessing you will get a quicker response over on Stack Overflow.
    – Steven D
    Commented Sep 21, 2010 at 4:20

1 Answer 1

4

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

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .