FILE *fd;
char File_name[]="";
<...>
printf("Enter the name of the file where you want the results to be saved. n");
printf("DON'T FORGET that file must end with .exe n");
scanf("%s",&File_name);
while(strchr(File_name,'.txt')==NULL)
{
printf("The end of the file name is not correct. Please try again. n");
printf("File name: ");
scanf("%s",&File_name);
}
警告:format指定类型"char",但参数的类型为"char(([1]"[-Wformat]scanf("%s",&文件名(;~~~~^~~~~~
箭头转到"&文件名"。
如何修复?非常感谢。
scanf()
期望char*
代替%s
。
File_name
具有类型char[1]
,因为它是一个元素数组并且该元素被初始化为' '
。
表达式中的大多数数组都转换为指针,但一元&
的操作数除外(本例(。
因此,&File_name
成为指向数组的指针,并且其类型为char(*)[1]
。
要修复此问题,请删除File_name
之前的&
。然后,数组File_name
将被转换为指向其第一个元素的char*
。
还有:
- 1元素肯定太短,无法读取字符串。通过指定
char File_name[512] = "";
等元素的数量来分配更多的元素 - CCD_ 15是一个多字符字符常量。它的价值是由实现定义的,不会是您想要的。您应该使用
strstr(File_name,".txt")
而不是strchr(File_name,'.txt')
。(strstr
用于搜索字符串(包括中间(,而不是检查后缀,但它的性能会比strchr()
好(