0

In C, a directory is created like this:

mkdir("hello");

but what if we don't know the name of this directory (or it's told by user)? How can we define it to a computer? (%s is not working)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
darya life
  • 13
  • 1
  • 5
  • 2
    When you say "`%s` is not working," what exactly do you mean? Are you trying to pass a 'format' string with that in directly to the `mkdir` function? – Adrian Mole Jun 19 '20 at 13:55
  • Note that POSIX [`mkdir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdir.html) takes two arguments — the directory name and a set of permissions to be assigned to it, as modified by the `umask` setting. – Jonathan Leffler Jun 19 '20 at 13:58
  • 1
    Your mention of `%s` suggests that maybe you should be using `snprintf()` to format a string to hold the name of the directory you want to create, and you then pass that string and the directory mode (e.g `0755`) to the `mkdir()` system call. See also [How can I create a directory tree in C++](https://stackoverflow.com/a/675193/15168) — which has an answer that is C code that can also be compiled by C++. – Jonathan Leffler Jun 19 '20 at 14:07
  • @JonathanLeffler the Visual C version of [`mkdir`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mkdir?view=vs-2019) and the later [`_mkdir`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mkdir-wmkdir?view=vs-2019) takes only one argument, although MS claims it has something to do with Posix. – Weather Vane Jun 19 '20 at 14:18
  • @WeatherVane — interesting. Fortunately, I did carefully identify "POSIX `mkdir()`", which does require two parameters. Since MS has a version that doesn't require those two parameters, I need to file that information away for future reference, and maybe the code would be OK if the platform was Windows or sufficiently closely related. And the relationship to POSIX is extra tenuous as MS requires header `` which is not a part of POSIX — the POSIX header for directory handling via `opendir()` et al is ``, and in any case `mkdir()` is declared in ``. – Jonathan Leffler Jun 19 '20 at 14:20
  • Rather than unknown use random word – Shivam Seth Jun 19 '20 at 15:03

3 Answers3

0

mkdir receives as parameter the name of the directory you want to create. Here the doc. So, you can define a variable to hold your input folder and pass it to the function

char path[30] = "path.to.dir";
mkdir(path, 0700);
  • It seems that this does not cover whatever OP needs about "%s is not working". – Yunnosch Jun 19 '20 at 13:56
  • 1
    Note that your code won't (shouldn't) compile — `mkdir()` takes two arguments. – Jonathan Leffler Jun 19 '20 at 14:02
  • What I understood was that he was trying to use mkdir as we use printf and scanf, replacing the value of the %d to the value of the variable. And I also understood that his question was related to the use of variables in a mkdir call – Mário Rocco Pettinati Jun 19 '20 at 14:02
  • I did this code in windows VS. It indeed used a deprecated version of mkdir, so you are right. I'll adjust the example – Mário Rocco Pettinati Jun 19 '20 at 14:04
  • 1
    Note the [comment](https://stackoverflow.com/questions/62471699/how-to-create-a-directory-with-unknown-name-using-c-language#comment110483195_62471699) from [Weather Vane](https://stackoverflow.com/users/4142924/weather-vane) — MS has a single-argument `mkdir()` function whereas POSIX has the two-argument version. The platform was not clearly identified — in different circumstances, we might both be right. – Jonathan Leffler Jun 19 '20 at 14:22
  • 1
    @MárioRoccoPettinati I believe the deprecation is only due to the naming convention for non-standard library functions, so `mkdir()` became `_mkdir()`. – Weather Vane Jun 19 '20 at 14:25
  • Yes. In VS, the suggest name is now _mkdir and it takes 1 parameter. In POSIX it takes two. But I agree with you all that we need more clarification in the question – Mário Rocco Pettinati Jun 19 '20 at 15:31
0

Just create a string variable, store the string in that variable (whether it's from user input or hardcoded), then pass the variable to mkdir.

int main() {
  char str[10];

  scanf("%9s", str);
  mkdir(str, 0700);

  return 0;
}
AJ_
  • 1,455
  • 8
  • 10
  • This almost certain to cause buffer overflow. – Weather Vane Jun 19 '20 at 13:57
  • It 's not so much the size, but that the input length is unrestricted. – Weather Vane Jun 19 '20 at 14:00
  • Note the [comment](https://stackoverflow.com/questions/62471699/how-to-create-a-directory-with-unknown-name-using-c-language#comment110483195_62471699) from [Weather Vane](https://stackoverflow.com/users/4142924/weather-vane) — MS has a single-argument `mkdir()` function whereas POSIX has the two-argument version. The platform was not clearly identified — in different circumstances, we might both be right. – Jonathan Leffler Jun 19 '20 at 14:23
0

I would recommend you to use snprintf so you can take any type of input.

#include <stdio.h>

int main() {
    char name[50];
    int i = 5;
    snprintf(name, 50, "dir.%i", 5);
    mkdir(name, 0700);
}
Akib Azmain Turja
  • 1,142
  • 7
  • 27