gpt4 book ai didi

c - 读取文件错误 C_language

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:16 24 4
gpt4 key购买 nike

我正在尝试创建文件并在其上写入一些数据,但是当我运行以下代码时程序运行出错:错误 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/

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