c - 读取文件错误 C_language

标签 c file fopen

我正在尝试创建文件并在其上写入一些数据,但是当我运行以下代码时程序运行出错:错误 1 ​​错误 C2664:'errno_t fopen_s(FILE **,const char *,const char *)' : 无法将参数 1 从“FILE *”转换为“FILE **”

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

FILE *myFile;


int main()
{
    int age;
    age = 24;
    fopen_s(myFile,"C:\\inetpub\\wwwroot\\DATA.EMP", "w");
    if (myFile == 0){
        printf("Error opening the file\n");
        exit(1);
    }
    fprintf(myFile, "I am %d years old \n", age);
    fclose(myFile);

    getchar();
    return 0;
}

可能是什么原因?

最佳答案

https://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx

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

因此,您的代码应该是:

fopen_s(&myFile,"C:\\inetpub\\wwwroot\\DATA.EMP", "w");

注意&myFile.

并检查你的返回值。

关于c - 读取文件错误 C_language,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29836138/

相关文章:

c++ - 抑制来自 CPD 的 C/C++ 代码警告

c - 以 64kb block 读取文件

c# - 复制文件集时出错

c - 我的错误是什么?

c - MSDN 对 fopen(或 fopen_s)的警告想表达什么?

c - 超线程代码示例

c - 列出链表

与删除 bst 函数中的指针混淆

swift - 在 Swift 中从 NSBundle 获取文件路径

c - 从文件保存和读取结构时出现问题