I want to make a C program that creates files for my lab class.
But with this code I am getting a warning with the fopen_s
function.
#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;
}
This is the warning I get with gcc:
How can I get rid of this warning?
You are using
fopen_s(&fp, name, "w");
According to https://en.cppreference.com/w/c/io/fopen , the function is declared in stdio.h
, which you are including, but it is only available since the C11 standard.
And,
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
.
So you need to enable C11 with -std=c11
if necessary (see How to enable c11 on later versions of gcc? ), and you need to define the __STDC_WANT_LIB_EXT1__
macro, if your compiler supports the function – which, as mentioned in the comments, seems unlikely. Otherwise, you cannot use the function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.