Visual Studio正在抱怨fopen。我找不到正确的语法来更改它。我有:
FILE *filepoint = (fopen(fileName, "r"));至
FILE *filepoint = (fopen_s(&,fileName, "r"));第一个参数的其余部分是什么?
发布于 2015-02-24 21:12:01
fopen_s是fopen的一个“安全”变体,它为模式字符串提供了一些额外的选项,并为返回流指针和错误代码提供了不同的方法。它是由微软发明的,并进入了C标准:它记录在最新的C11标准草案的附件K.3.5.2.2中。当然,在Microsoft联机帮助中有完整的文档记录。您似乎不理解在C中传递指向输出变量的指针的概念。在您的示例中,您应该传递filepoint的地址作为第一个参数:
errno_t err = fopen_s(&filepoint, fileName, "r");下面是一个完整的示例:
#include <errno.h>
#include <stdio.h>
#include <string.h>
...
FILE *filepoint;
errno_t err;
if ((err = fopen_s(&filepoint, fileName, "r")) != 0) {
// File could not be opened. filepoint was set to NULL
// error code is returned in err.
// error message can be retrieved with strerror(err);
fprintf(stderr, "cannot open file '%s': %s\n",
fileName, strerror(err));
// If your environment insists on using so called secure
// functions, use this instead:
char buf[strerrorlen_s(err) + 1];
strerror_s(buf, sizeof buf, err);
fprintf_s(stderr, "cannot open file '%s': %s\n",
fileName, buf);
} else {
// File was opened, filepoint can be used to read the stream.
}微软对C99的支持是笨拙和不完整的。Visual Studio为有效代码生成警告,强制使用标准但可选的扩展,但在这种特定情况下似乎不支持strerrorlen_s。有关更多信息,请参阅Missing C11 strerrorlen_s function under MSVC 2017。
https://stackoverflow.com/questions/28691612
复制相似问题