c - 为什么我会收到警告 "implicit declaration of function ' fopen_s'"以及如何摆脱它?

标签 c

我想编写一个 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 中收到的警告:

1

如何消除这个警告?

最佳答案

您正在使用

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 including stdio.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/

相关文章:

c - 可能会失败的分配和操作顺序的正确模式

c - 欧拉在 C 中的 totient 函数

c - 如何在 C 中以 O(1) 时间合并两个链表?

c - 在 3 位数字上使用逻辑或关系运算符进行素数测试

c - 使用字符指针修改字符串

C# 调用返回类型为 struct* 的 c dll 函数(我没有 struct c 代码/定义)

javascript - OPC 服务器到 MySQL 数据库

c - 什么时候应该使用 select 与多线程进行比较?

c - 如何计算c中的逆模幂?

C从一个文件读取数据并将计算结果存储在另一个文件中