6

I am trying to create a file in C by trying to execute the following code segment, but I am getting an "identifier "mkdir" is not defined". I am working on a Windows Machine using Visual Studio.

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

    int main()

          {

   char newTempFolderName[50];

  int a = mkdir("./newTempFolderName", 0700);

    return 0;

          } 
Abhinav Kumar
  • 63
  • 1
  • 1
  • 4
  • It is working in ubuntu. In your program what is need of character array.. Because you are not used array.If want to create directory for array content, initialize or scan the array first,then use array in 'mkdir' system call like 'mkdir(newTempFolderName,07000). – Anbu.Sankar Sep 16 '14 at 11:31
  • For future reference, a simple search of `mkdir` on [msdn](http://msdn.microsoft.com) comes up with [mkdir](http://msdn.microsoft.com/en-us/library/ms235326.aspx) – crashmstr Sep 16 '14 at 11:40
  • @PaulR: last time I checked, mkdir wasn't in the C standard? – Harry Johnston Sep 16 '14 at 23:16
  • @HarryJohnston: I'm not sure about C, but it's part of POSIX. – Paul R Sep 17 '14 at 05:32
  • @PaulR: sure, but POSIX is a UNIX standard. Calling Windows "non-standard" because it doesn't implement POSIX is like calling Java non-standard because it doesn't implement C11. :-) – Harry Johnston Sep 17 '14 at 22:27
  • @HarryJohnston: OK - I admit it - I was being a little wasp-ish about Windows due to personal prejudice. ;-) – Paul R Sep 18 '14 at 05:36

2 Answers2

10

Use WinApi's CreateDirectory() function or use _mkdir() (notice the underscore sign).

Example of CreateDirectory() - you need to include windows.h header file:

#include<windows.h>

int main() {
   CreateDirectory ("C:\\test", NULL);
   return 0;
}
macfij
  • 3,093
  • 1
  • 19
  • 24
9

Try this:

 #if defined(_WIN32)
    _mkdir("./newTempFolderName");
     #else 
    mkdir("./newTempFolderName", 0700); 
     #endif
Marco Tulio
  • 145
  • 10