• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

create a new directory

 How can I create a new directory using ANSI C++ in both UNIX and Windows?

Thanks in advance for your answer(s)

Christos
0
Celetron
Asked:
Celetron
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

The standard C library (of which C++ is wrapped) contain the mkdir() API which is common with unix.


mkdir (char *DirectoryName);


Should just about do it.


Kdo
0
 
CeletronAuthor Commented:
thank you Kdo. it worked under win 2000. the code i used is the following:

#include <stdio.h>
#include <stdlib.h>    
#include <sys/types.h>
#include <sys/stat.h>    
#include <fcntl.h>  

int main(int argc, char *argv[])
{
  char *DirectoryName = "C:\\NewDir";
  mkdir(DirectoryName);

  return 0;
}

However, it does not work for the creation of two subdirectories at the same time. For instance, if i write

char *DirectoryName = "C:\\NewDir\\AnotherDir";

instead of

char *DirectoryName = "C:\\NewDir";

it doesn't work.

Do you know if there is a way to overcome this problem or do i have to create each subdirectory individually?

Thanks again

Christos
0
 
CeletronAuthor Commented:
the necessary header files are the

#include <stdio.h> and the
#include <fcntl.h>  
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
efnCommented:
mkdir is not part of the standard C library, though it is provided for both Windows and UNIX.  See this discussion:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_11509318.html

The documentation for Windows says explicitly that it can only create one directory at a time:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__mkdir.2c_._wmkdir.asp

The Open Group documentation for the UNIX version says it should fail if any part of the path prefix doesn't exist, which amounts to the same thing:

http://www.opengroup.org/onlinepubs/007904975/functions/mkdir.html

So I think you have to create each subdirectory individually, assuming you want to stick with mkdir.  There may be less portable functions that do what you want.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Yeah -- Saying that it was part of the "standard" C library was inaccurate.  It is a part of one of the "extended" headers/libraries and should be available on both platforms.


Kdo
0
 
CeletronAuthor Commented:
I have tested the code on both win 2000 and Me and it worked fine. Unfortunately, in order to run the program under UNIX i have to compile it by using a UNIX compiler. For the moment, i cannot figure out how to make the code cross-platform. Therefore, i will leave the question unlocked for a couple of weeks just in case somebody can help me with that. Otherwise, i will give the points to Kdo since he answered first unless if there is a way to share the point to both of u.

Thank you both Kdo and efn for your answers.

Christos
0
 
Kent OlsenData Warehouse Architect / DBACommented:

What kind of problems are you having making "the code cross-platform"?

Most unix C compilers support the C++ extensions so the "base" code in your application should move to unix fairly easily.  However, if your application is a Window's GUI you might have quite a bit of work ahead of you moving it to X.


Kdo
0
 
efnCommented:
That's what you get for asking two (now three) questions on one page.  :-)  But I must say Kdo not only answered first, but answered the more general and important question.

As Kdo said, we can't help you make the code cross-platform unless you tell us more about what your current problem is.

If you need to use a drive letter in the pathname, there may be an ingenious solution, but I don't know what it is, except perhaps platform-dependent conditional compilation, which is ugly, but common.

If you are concerned about the delimiters in the pathname, I believe Windows will accept forward slashes and UNIX will not accept backslashes, so forward slashes would be the portable delimiters.

--efn
0
 
Kent OlsenData Warehouse Architect / DBACommented:
[off topic]

char FileNameBuffer[MAX_PATH+3]
char *FileName;
char DriveString[4] = "C:\\";
...

FileName = FileNameBuffer+3;

/*  Build/copy file name to FileName  */

if (DriveLetterRequired)
{
  if (*(FileName+1) != ':')
  {
    FileName = FileNameBuffer;
    memcpy (FileName, DriveString, 3);
  }
}

[/off topic]


Too many years building operating systems, I guess.  ;)

Kdo

0
 
Makaveli_7Commented:
Hi,

 Creating a Directory


After writing your source code, your program may be developed into an executable program. That program can then be executed on any computer running Windows 95 (or higher) or Windows NT. The Visual C++ Development Studio will automatically create and maintain all the files required to build an executable program. The Visual C++ Development Studio will store them in a Project file. To build a Visual C++ Project, follow the following steps:
It is a good idea to create a directory for storing your Visual C++ projects. Subdirectories for storing each project can then be created within this directory. To create this directory, follow these simple steps:
 

1-Click on the "Start" button with the left mouse button.
2-Select "Programs" from the pop-up menu.
3-Then select "Windows XP."
4-Now single-click on "Afs[H:]" in Explorer.
5-Select the "File" menu.
6-Now Select "New" and then "New Folder" from the pop-up menu.
7-In the new folder's name area now highlighted in blue, type the name you would like to call your folder and hit "enter". For example, I named my directory test_ex.


I assume you know how to build a project. I also assume you know how to add source code to your project.


Inserting Dialog Boxes into Your Project------


Once you have a project created a dialog box can be inserted. A dialog box is a good way of getting input information into your program. To add a dialog box to your project follow these steps:


1-In Visual C++ select "Insert" from the Tool bar.
2-Now select "Resource".
3-This will bring up the "Insert Resource" window.
4-Click on "Dialog" (or the "+" beside it if you want more advanced options) and select "New".
Now two new windows should open: a script file window that displays a prototype of your dialog box, and a tool bar with controls for the dialog box.
5-Now, you can design and test the dialog box. To test your dialog bow simply select "Test" from the "Layout" menu.


I hope this helps.

Asad.
 
0
 
Makaveli_7Commented:
Hi, above I explained in Visual C++, but you asked in ANSI C++. To create in ANSI C++:

Create a directory or navigate to where you want to store your source.
 For example:
 3.  Type "cd"
 4.  Type "mkdir MySource"
 5.  cd Mysource
 (Now in c:\MySource)
 
 Create a new source file:
 For example:
 6.  Type "edit hello.cpp"
 7. Paste or type the following code in the editor:

  #include <iostream.h>
  int main(void)
  {
   cout << "Hello." << endl;
   return 0;
  }

 8. Save the changes (Alt-F then hit S).
 9. Exit edit. (Alt+F then press X).

 Compiling the program to create an executable:
 10. Type "bcc32 hello.cpp"

 Now, run the application you created.
 11. Type "hello"
 (The output will appear below your last command line.)

0
 
CeletronAuthor Commented:
thx all for your time :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now