我想通过导入用户的文件名来创建文件,请帮忙
int file_name;
printf("Enter ID NUMBER : ");
scanf("%d",&file_name);
FILE *fin;
fin = fopen(&file_name , "w");
最佳答案
这里
FILE *fin;
fin = fopen(&file_name , "w"); /* this is wrong, since &file_name is of int* type */
fopen()
需要 char*
类型的第一个参数,但您提供的 int*
类型是错误的,并且有被编译器正确报告为
error: incompatible pointer types passing
'int *'
to parameter of type'const char *'
[-Wincompatible-pointer-types]
如果你可以用像这样的标志编译
-Wall -Wpedantic -Werror
。来自 fopen() 的手册页
FILE *fopen(const char *pathname, const char *mode);
将 file_name
声明为字符数组 并将文件名存储到其中。
char file_name[1024]; /* take a char array to store file name */
/* @TODO : store actual file name into file_name array */
FILE *fin = fopen(file_name , "w");
if(fin == NULL) {
/* @TODO : error handling */
}
关于c - 如何编写一个c程序来创建一个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835510/