Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir Windows vs Linux

I have a problem while porting a Linux tool to Windows. I am using MinGW on a Windows system. I have a class which handles all the in/output and within is this line:

mkdir(strPath.c_str(), 0777); // works on Linux but not on Windows and when it is changed to
_mkdir(strPath.c_str()); // it works on Windows but not on Linux

Any ideas what I can do, so that it works on both systems?

like image 666
MindlessMaik Avatar asked Apr 27 '12 19:04

MindlessMaik


People also ask

Can you use mkdir for Windows?

The mkdir (make directory) command in the Unix, DOS, DR FlexOS, IBM OS/2, Microsoft Windows, and ReactOS operating systems is used to make a new directory. It is also available in the EFI shell and in the PHP scripting language. In DOS, OS/2, Windows and ReactOS, the command is often abbreviated to md .

What is mkdir P in Windows?

mkdir - p creates the directory in the path mentioned by you explicitly. mkdir temp will create the directory in c:\users\Administrator ( current directory for me) mkdir -path c:\temp - will create directory in C:\ drive, irrespective of my current working directory.

How does mkdir work in Linux?

mkdir command in Linux allows the user to create directories (also referred to as folders in some operating systems ). This command can create multiple directories at once as well as set the permissions for the directories.

What is mkdir P?

Linux Directories mkdir -p With the help of mkdir -p command you can create sub-directories of a directory. It will create parent directory first, if it doesn't exist. But if it already exists, then it will not print an error message and will move further to create sub-directories.


3 Answers

#if defined(_WIN32)
_mkdir(strPath.c_str());
#else 
mkdir(strPath.c_str(), 0777); // notice that 777 is different than 0777
#endif
like image 171
gcochard Avatar answered Sep 29 '22 01:09

gcochard


You should be able to use conditional compilation to use the version that applies to the OS you are compiling for.

Also, are you really sure you want to set the flags to 777 (as in wide open, please deposit your virus here)?

like image 22
Eric J. Avatar answered Sep 29 '22 02:09

Eric J.


You can conditionally compile with some preprocessor directives, a pretty complete list of which you can find here: C/C++ Compiler Predefined Macros

#if defined(_WIN32)
    _mkdir(strPath.c_str());
#elif defined(__linux__)
    mkdir(strPath.c_str(), 0777);
// #else more?
#endif
like image 1
逆さま Avatar answered Sep 29 '22 00:09

逆さま