[英]How to create a file in a directory where the name of such directory is specified in a command line argument in C
我已经搞定了:
FILE *fp;
fp = fopen("../argv[2]/dummyfile", "w");
但它实际上需要“argv [2]”。 我如何使它不按字面意思理解?
您可以使用sprintf
创建所需的文件路径字符串。
int lenbuf = FILENAME_MAX;
char filePath[lenbuf];
int n = snprintf(filePath, lenbuf, "../%s/dummyfile", argv[2]);
if(n > -1 && n < lenbuf){
fp = fopen(filePath, "w");
}else{
printf("Error while creating path from input arguments");
// handle the error here, user default or do exit/return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.