Solved

Remove a file

Posted on 2004-11-01
181 Views
Last Modified: 2010-04-01
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;
}
0
Question by:jewee
    1 Comment
     
    LVL 14

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now