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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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

765 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