PDA

View Full Version : Creating, deleting folders with C++?



pir
18th July 2006, 10:01
Hi!

I need to be able to create and delete folders from my program. But I can't find out how to do this. For UNIX, and also Linux I suppose, there should be some function like mkdir... But I can't find anything about it. And the home directory, how do I find it?

Since my program is supposed to work on UNIX, Linux, Windows and MacOS I need to know how to do this on all those operating systems. How is the best way to implement it to compile the right creation function depending on which operating systems the compilation is made on?
Some kind of macro maybe? Like:

#if WIN32
void createWindowsFolder();
#endif

#if LINUX
void mkdir
#endif

#if MACOS
void mkdir()?
#endif


thanks for reading
pir

guilugi
18th July 2006, 10:40
Hi,

I have to check and grab my code, but I implemented those functions with Qt base IO function.

1)
In Qt, you have a mkDir() method in QDir class, or also mkPath().
So, mkdir is quite easy to do, on every platform ;-)
You don't have to do all those if...

2)
For deleteDir() : simple too !
You just have to do a recursive method(), that delete all subfolders and files....and then delete current dir.

I'll look for my old dusty code and keep you in touch ;)

Guilugi.

pir
18th July 2006, 10:54
Thanks!
That seems to be a good way to do it. But I would also like to know how to do this without Qt, because I want to make it easy to change Qt with something else if that is necessary.
But it is nice to know that I can do it with Qt if I'm getting tired of looking for a solution.

I found out that getenv() makes it possible to get the home directory for Linux.


thanks
pir

Kumosan
1st August 2006, 10:19
Hi!

I need to be able to create and delete folders from my program. But I can't find out how to do this. For UNIX, and also Linux I suppose, there should be some function like mkdir... But I can't find anything about it.
pir

man 2 mkdir
man 2 rmdir

sumsin
1st August 2006, 11:30
can we create directories through C/C++?

jacek
1st August 2006, 11:36
can we create directories through C/C++?
There are POSIX functions for this.

man 2 mkdir
man 2 rmdir

;)

sumsin
1st August 2006, 11:53
but does POSIX functions will be compatible on WINDOWS.

as C/C++ are not Operating System dependent.

ANd Qt also using C++.

Then is there something in c++ through which we can create folders?

jacek
1st August 2006, 12:18
but does POSIX functions will be compatible on WINDOWS.
windows implements small part of POSIX, so it might be available.

http://search.msdn.microsoft.com/search/default.aspx?__VIEWSTATE=&query=mkdir+C%2B%2B&siteid=0&tab=0


ANd Qt also using C++.
QDir


Then is there something in c++ through which we can create folders?
No, pure C and C++ can operate only on files. I guess it's part of being "OS independent".