我用的是 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_s
和 fopen
。 fopen_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/