[英]How to create a directory with unknown name using C language
In C, a directory is created like this:在 C 中,这样创建一个目录:
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)
(%s 不工作)
mkdir receives as parameter the name of the directory you want to create. mkdir 接收您要创建的目录的名称作为参数。 Here the doc .
这里是文档。 So, you can define a variable to hold your input folder and pass it to the function
因此,您可以定义一个变量来保存您的输入文件夹并将其传递给 function
char path[30] = "path.to.dir";
mkdir(path, 0700);
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
.只需创建一个字符串变量,将字符串存储在该变量中(无论是来自用户输入还是硬编码),然后将变量传递给
mkdir
。
int main() {
char str[10];
scanf("%9s", str);
mkdir(str, 0700);
return 0;
}
I would recommend you to use snprintf
so you can take any type of input.我建议您使用
snprintf
以便您可以接受任何类型的输入。
#include <stdio.h>
int main() {
char name[50];
int i = 5;
snprintf(name, 50, "dir.%i", 5);
mkdir(name, 0700);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.