Creating, deleting folders with C++?
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:
Code:
#if WIN32
void createWindowsFolder();
#endif
#if LINUX
void mkdir
#endif
#if MACOS
void mkdir()?
#endif
thanks for reading
pir
Re: Creating, deleting folders with C++?
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.
Re: Creating, deleting folders with C++?
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
Re: Creating, deleting folders with C++?
Quote:
Originally Posted by pir
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
Re: Creating, deleting folders with C++?
can we create directories through C/C++?
Re: Creating, deleting folders with C++?
Quote:
Originally Posted by sumsin
can we create directories through C/C++?
There are POSIX functions for this.
man 2 mkdir
man 2 rmdir
;)
Re: Creating, deleting folders with C++?
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?
Re: Creating, deleting folders with C++?
Quote:
Originally Posted by sumsin
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/sea...siteid=0&tab=0
Quote:
Originally Posted by sumsin
ANd Qt also using C++.
QDir
Quote:
Originally Posted by sumsin
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".