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?
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 .
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.
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.
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.
#if defined(_WIN32)
_mkdir(strPath.c_str());
#else
mkdir(strPath.c_str(), 0777); // notice that 777 is different than 0777
#endif
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)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With