Remove a file

I wrote a function to clear out a directory.  It will remove filenames that begin with CASE or CONFIG.   It recognizes the file, but does not remove it.  Here is the code.  I am running on a solaris machine...
const char * const CASE_FILE = "CASE";
const char * const CONFIG_FILE = "CONFIG";
bool ViewForm::cleanDirectory()
{

   DIR* dirp;
   dirent* dp;
   if ((dirp=opendir(msgFilePath)) == NULL)
   {
       printf("Error polling directory\n");
       return false;
   }

   while ((dp = readdir(dirp)))
   {    printf("file name  %s\n", dp->d_name);
        if (strstr(dp->d_name, CASE_FILE) ||
            strstr(dp->d_name, CONFIG_FILE))
        {  
            printf("Match!\n");
            remove(dp->d_name);
        }
    }
   return true;
}
jeweeAsked:
Who is Participating?
 
RichieHindleConnect With a Mentor Commented:
You're opening msgFilePath explicitly, which implies that it's not your current working directory.  But then you're passing simple directory names rather than full pathnames to remove().  You need to either set your working directory to msgFilePath:

   chdir(msgFilePath);

or build full pathnames for remove():

  char fullpath[1000];
  sprintf(fullpath, "%s/%s", msgFilePath, dp->d_name);
  remove(fullpath);
0
All Courses

From novice to tech pro — start learning today.