Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


get Directory Info

Posted on 2003-03-24
Medium Priority
Last Modified: 2008-02-20
In C++ I want to check if a directory exists, if not create it.  
I thought I could use mkdir but on MSDN it doesnt mention compatibily with win XP???
can I use create directory to perform the above?

Question by:oliverUK
LVL 48

Accepted Solution

AlexFM earned 150 total points
ID: 8194095
You can use mkdir, internally it calls platform-specific function. It will work on XP. You don't need to check if directory exists, just call the function. If directory exists, the function returns -1 and sets errno EEXIST.

LVL 12

Expert Comment

ID: 8194417
One very useful function is a function that creates a file (if it doesn't exist) and also create any directories that doesn't exist in order to create the file at that directory.

FILE * ensure_file_exist(const char * f, const char * m);

Now, you can change the string to const string & f if you want to and the return value can be a HANDLE if you want to create the file using Win32 function CreateFile or CreateFileEx.

To make this compatible with fstream however is more tricky, the reason is that fstream::open() isn't a virtual function and besides it just calls filebuf::open. The filebuf::open is a virtual function so if you want to make your own filebuf just to implement this (and also make your own fstream) you can do that, but it is rather much work. It is probably easier in that case to simply just open the file using this funciton and then immediately close the file using fclose and then do fstream::open() on the file.

int ensure_file_exist(const char * ff, const char * m)
   int len = strlen(ff);
   char * f = new char[len + 1];

   const char * p = strcpy(f,ff);

   // if on windows we can check for C: etc...
   // we could also have made check for UNC names,
   // I leave that as an exercize to the reader.
   // Under unix you will skip this test.
   // Under windows you might also want to check for
   // \ in addition to /, I leave that too as
   // an exercize for the reader.
   if (p[1] == ':')
      p += 2;
   if (*p == '/')
   const char * q;

   while ((q = strchr(p,'/')) != 0) {
      // f..q holds a directory path.
      *q = 0; // replace the / with '\0'.
      *q++ = '/'; // put the / back.
      p = q;
   delete [] f;
   return fopen(ff,m);

The function as written does very little error checking, for example mkdir() isn't checked at all - either we got the directory and everything's fine or the directory existed already and also everything's fine or we got some other error and in that case we do not really handle it at all and rely on caller to detect the situation.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

564 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