Is there a way to use fopen_s() with GCC or at least create a #define about it?

前端 未结 6 925
遇见更好的自我
遇见更好的自我 2020-12-05 10:54

MSVC compiler says that fopen() is deprecated, and recommends the use of fopen_s().

Is there any way to use fopen_s() and stil

6条回答
  •  余生分开走
    2020-12-05 11:40

    In C/C++ code,

    #ifdef __unix
    #define fopen_s(pFile,filename,mode) ((*(pFile))=fopen((filename),(mode)))==NULL
    #endif
    

    In Makefile

    CFLAGS += -D'fopen_s(pFile,filename,mode)=((*(pFile))=fopen((filename),(mode)))==NULL'
    

    Attention that on success fopen_s return 0 while fopen return a nonzero file pointer. Therefore it is necessary to add "==NULL" to the end of macro, e.g.:

    if (fopen_s(&pFile,filename,"r")) perror("cannot open file");
    

提交回复
热议问题