2

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:

1

How can I get rid of this warning?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 5
    Use `fopen` instead of `fopsn_s`. Latter is a non standard thing and seems not to be implemented on your platform. `fopen_s(&fp , name , "w")` -> `fp = fopen( name , "w")`. Also check if `fp` is `NULL` after `fopen`, this means the file could not be opened. – Jabberwocky Feb 01 '21 at 13:57
  • 1
    "Implicit declaration" warning means the compiler has not yet seen a prototype in a header file describing the function. This likely means your compiler does not support this function. – PeterT Feb 01 '21 at 13:57
  • 2
    BTW: each time you see a warning containing the word _implicit_, consider it as an error. – Jabberwocky Feb 01 '21 at 13:58
  • 4
    @Jabberwocky Pedantically, `fopen_s()` is standardized under the optional Annex K of the C standard. But no notable implementations other than Microsoft's exist, and Microsoft's implementation doesn't follow Annex K anyway. ;-) – Andrew Henle Feb 01 '21 at 14:05
  • 2
    FWIW, @Jabberwocky's comment is actually the answer here. – Andrew Henle Feb 01 '21 at 14:11
  • @Jabberwocky Your comment was very helpful and it also works. Also i want to use ansi c beacuse in my university we learn ansi c and my project i want to be in ansi c. In addition how can i check if fp is NULL? You mean that i need to put manually null into fp? – Κωνσταντίνος Ντούρος Feb 01 '21 at 14:20
  • @PeterT Ok, so this is the reason why my compiler doesnt recognizes fopen_s. Btw does take part that my compiler is for ansi c? or not? Sorry for my english iam from greece. – Κωνσταντίνος Ντούρος Feb 01 '21 at 14:23
  • 1
    @ΚωνσταντίνοςΝτούρος non, like this `if (fp == NULL) { printf("File could not be opened") ; exit(1);}`. If the file could n bot be opened, it's pointless to continue, you need to abort. And you are already using ANSI C, non ANSI C hasn't been in use for the last 20 (or 25 or even more) years. – Jabberwocky Feb 01 '21 at 14:29
  • 2
    @ΚωνσταντίνοςΝτούρος ... Just learn how to use `fopen`. It will make your life a lot easier. :) – PeterT Feb 01 '21 at 20:57

1 Answers1

2

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

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    No. [`fopen_s()` is part of the **optional** Annex K](https://port70.net/~nsz/c/c11/n1570.html#K.3.5.2.1). And per [**Field Experience With Annex K — Bounds Checking Interfaces**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1967.htm): "Microsoft Visual Studio implements an early version of the APIs. However, the implementation is incomplete and conforms neither to C11 nor to the original TR 24731-1. ... As a result of the numerous deviations from the specification the Microsoft implementation cannot be considered conforming or portable." – Andrew Henle Feb 01 '21 at 14:08
  • 4
    There are really no other implementations of Annex K in widespread use. Using the functions in Annex K effectively makes your code non-portable. – Andrew Henle Feb 01 '21 at 14:10
  • @mkrieger1 You answer is very helpfull but iam using ansi c and i dont know if your answer follows ansi c. – Κωνσταντίνος Ντούρος Feb 01 '21 at 14:40
  • 1
    `fopen_s` is definitely not available in ANSI C ([aka C89](https://en.wikipedia.org/wiki/ANSI_C)). – mkrieger1 Feb 01 '21 at 15:19