How to create a folder in C (need to run on both Linux and Windows)

Aleksandair picture Aleksandair · Apr 24, 2014 · Viewed 10.3k times · Source

I don't have much experience and I'm on a C project where I need to create & delete folders and the program must run on both Linux and Windows.

I saw few solutions but all were either for Windows or Linux but none for both and most uses system(...).

Also, if there is an easy way to delete a folder with it's contents, I'm interrested (for the moment I delete each files one by one and then the folder with remove(...)) Thanks in advance.

Answer

Mahonri Moriancumer picture Mahonri Moriancumer · Apr 24, 2014

Here is a common 'create directory' method:

void make_directory(const char* name) 
   {
   #ifdef __linux__
       mkdir(name, 777); 
   #else
       _mkdir(name);
   #endif
   }

As for removing directories, you are on the right track, ie:

for the moment I delete each files one by one and then the folder with remove(...)