我知道如何创建文件,但无法创建扩展名为 .blabla 的文件。这不可能吗?
谢谢
最佳答案
您可以使用固定长度和 snprintf
,如 Tim Cooper 所说,或者您可以使用 malloc
和 string.h
中包含的字符串函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
#define EXT ".blabla"
char *s;
FILE *f;
if (argc < 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
s = malloc(strlen(argv[1]) + strlen(EXT) + 1);
if (s == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
strcpy(s, argv[1]);
strcat(s, EXT);
f = fopen(s, "w");
if (f == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
fclose(f);
free(s);
return 0;
}
关于c - 如何创建文件名为 argv[1].blabla 的文件?在c中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17445817/