简体   繁体   English

C中的VS2013文件打开错误

[英]VS2013 file open error in C

i use a vs2013. 我使用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);
}

but Error 4 error C4700: uninitialized local variable 'f' used I saw-_- how must i change? 但错误4错误C4700:我看到未初始化的局部变量'f',我看到了-_-我该如何更改?

You seem to be confusing fopen_s and fopen . 您似乎将fopen_sfopen混淆了。 The signature of fopen_s is: fopen_s的签名是:

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

And the signature of fopen is: fopen的签名是:

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

The way you use these two functions respectively is: 您分别使用这两个功能的方式是:

FILE* f = NULL;
errno_t e;

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

and: 和:

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

If an error occurs, fopen sets errno . 如果发生错误,则fopen设置errno

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM