我想编写一个 C 程序来为我的实验室类(class)创建文件。
但是通过这段代码,我收到了 fopen_s
函数的警告。
#include <stdio.h>
int main(void)
{
int i , j , seira , sum , temp;
printf("Give the number of exercise series:\n");
scanf("%d" , &seira);
printf("Give me the number of total exercises\n");
scanf("%d" , &sum);
FILE *fp;
char name[FILENAME_MAX];
j = seira;
temp = sum;
if (j < 10)
{
for (i = 1; i <= temp; i++)
{
_snprintf(name , sizeof(name) , "Homework0%d_Group03_%d.c", j , i);
fopen_s(&fp , name , "w");
fclose(fp);
}
}
else if (j >= 10)
{
for (i = 1; i <= temp; i++)
{
_snprintf(name , sizeof(name) , "Homework%d_Group03_%d.c", j , i);
fopen_s(&fp , name , "w");
fclose(fp);
}
}
return 0;
}
这是我在 gcc 中收到的警告:
如何消除这个警告?
最佳答案
您正在使用
fopen_s(&fp , name , "w");
根据https://en.cppreference.com/w/c/io/fopen ,该函数在您所包含的 stdio.h
中声明,但它仅自 C11 标准起可用。
并且,
fopen_s
is only guaranteed to be available if__STDC_LIB_EXT1__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__
to the integer constant 1 before includingstdio.h
.
因此,如有必要,您需要使用 -std=c11
启用 C11(请参阅 How to enable c11 on later versions of gcc? ),并且您需要定义 __STDC_WANT_LIB_EXT1__
宏,如果 你的编译器支持这个函数——正如评论中提到的,这似乎不太可能。否则,您无法使用该功能。
关于c - 为什么我会收到警告 "implicit declaration of function ' fopen_s'"以及如何摆脱它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65993902/