?
Solved

create a new directory

Posted on 2003-03-13
12
Medium Priority
?
209 Views
Last Modified: 2010-04-01
 How can I create a new directory using ANSI C++ in both UNIX and Windows?

Thanks in advance for your answer(s)

Christos
0
Comment
Question by:Celetron
[X]
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
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 8128930

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
 

Author Comment

by:Celetron
ID: 8129733
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
 

Author Comment

by:Celetron
ID: 8129779
the necessary header files are the

#include <stdio.h> and the
#include <fcntl.h>  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:efn
ID: 8130138
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8131413

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
 

Author Comment

by:Celetron
ID: 8135184
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8135836

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
 
LVL 15

Expert Comment

by:efn
ID: 8136472
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8136581
[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
 

Expert Comment

by:Makaveli_7
ID: 8140323
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
 

Expert Comment

by:Makaveli_7
ID: 8140372
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
 

Author Comment

by:Celetron
ID: 8140558
thx all for your time :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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