Visual Studio 2013 // f = fopen_s(“text.txt”,“r”) - 不起作用

时间:2015-04-10 09:00:30

标签: file visual-studio-2013

我知道有两种使用fopen的方法:

  1. fopen_s(& f,name,mode);
  2. f = fopen_s(名称,模式);
  3. 第一个在visual studio 2013中工作 第二个没有,我得到一个错误说明:

    错误:无法将类型为“errno_t”的值分配给“FILE”类型的实体

    代码应该找到一个文件并在屏幕上打印文件中的数字或退出代码,当我使用第一个选项fopen_s(&f, "text.txt", "r");时,代码直接转到NULL。该文件位于默认文件夹中。

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    void main()
    {
    int n;
    FILE *f;
    
    f = fopen_s("text.txt", "r");
    
    if (!f)
    
    { 
            printf_s("\nError reading file");
            exit(1);
    }
    
    else
    
    { 
        fscanf_s(f, "%d", &n);
        printf_s("The file contains no: %d", n);
    
        fclose(f);
    }
    
    _getch();
    }
    

1 个答案:

答案 0 :(得分:0)

它没有用,因为使用它的第二种方法是不正确的。 fopen_s不返回FILE *。另一方面,fopen以您认为第二个版本的方式工作。您的选择是fopen_s(&f, "text.txt","r");f = fopen("text.txt","r");