gpt4 book ai didi

c - C中VS2013文件打开错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:30:56 24 4
gpt4 key购买 nike

我用的是 vs2013。

#include <stdio.h>

void main(){

FILE *f;
char fname[] = "17_1.txt";

if ((f = fopen_s(f,fname, "r+")) == NULL)
{
fprintf(stdout, "can't open the file\n");
exit(1);
}
while (!feof(f))
{
fscanf_s(f, "%d %s %d %d\n");
fprintf(stdout, "%d %s %d %d \n");
}

fclose(f);
}

但错误 4 error C4700: 使用了未初始化的局部变量 'f'我看见-_-我该如何改变?

最佳答案

您似乎混淆了 fopen_sfopenfopen_s 的签名是:

errno_t fopen_s( 
FILE** pFile,
const char *filename,
const char *mode
);

fopen 的签名是:

FILE *fopen(const char *restrict filename, const char *restrict mode);

这两个函数的使用方式分别是:

FILE* f = NULL;
errno_t e;

if ((e = fopen_s(&f, fname, "r+")) != 0)
{
fprintf(stdout, "can't open the file\n");
}

和:

if ((f = fopen(fname, "r+")) == NULL)

如果发生错误,fopen 设置errno

关于c - C中VS2013文件打开错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28402614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com