VS 2019版本,fopen 函数使用报错,要传3个参数,而且有个啥errno_t是啥意思啊

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    FILE* fp;
    errno_t err;
    char str[100];
    int i = 0;
    if ((err = fopen_s(&fp,"test", "w")) == NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
    printf("input a string:\n");
    gets_s(str);
    while (str[i] != '!')
    {
        if (str[i] >= 'a' && str[i] <= 'z')
            str[i] -= 32;
        i++;
    }
    fputs(str, fp);
    fclose(fp);
    err = fopen_s(&fp,"test", "r");
    fgets(str, strlen(str) + 1, fp);
    printf("%s\n", str);
    fclose(fp);
    return 0;
}

这个我点完开始运行咋跳出的是“cannot open file”啊,求解

errno_t是一个类型,存储错误信息,fopen_s是fopen的安全版本。关键是,你有没有那个文件?

修改如下,供参考:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE* fp;
    errno_t err;
    char str[100];
    int i = 0;
    if ((err = fopen_s(&fp, "test", "w")) != 0)//if ((err = fopen_s(&fp, "test", "w")) == NULL)
    {
        printf("cannot open file\n");
        exit(0);
    }
    printf("input a string:\n");
    gets_s(str);
    while (str[i] != '!')
    {
        if (str[i] >= 'a' && str[i] <= 'z')
            str[i] -= 32;
        i++;
    }
    fputs(str, fp);
    fclose(fp);
    if ((err = fopen_s(&fp, "test", "r")) != 0)
    {
        printf("cannot open file\n");
        exit(0);
    }
    fgets(str, strlen(str) + 1, fp);
    printf("%s\n", str);
    fclose(fp);
    return 0;
}