Visual Studio is complaining about fopen. I can't find the proper syntax for changing it. I have:
FILE *filepoint = (fopen(fileName, "r"));
to
FILE *filepoint = (fopen_s(&,fileName, "r"));
What is the rest of the first parameter?
Visual Studio is complaining about fopen. I can't find the proper syntax for changing it. I have:
FILE *filepoint = (fopen(fileName, "r"));
to
FILE *filepoint = (fopen_s(&,fileName, "r"));
What is the rest of the first parameter?
fopen_s
is a "secure" variant of fopen
with a few extra options for the mode string and a different method for returning the stream pointer and the error code. It was invented by Microsoft and made its way into the C Standard: it is documented in annex K.3.5.2.2 of the most recent draft of the C11 Standard. Of course it is fully documented in the Microsoft online help. You do not seem to understand the concept of passing a pointer to an output variable in C. In your example, you should pass the address of filepoint
as the first argument:
errno_t err = fopen_s(&filepoint, fileName, "r");
Here is a complete example:
#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.
...
fclose(filepoint);
}
Microsoft's support for C99 is clunky and incomplete. Visual Studio produces warnings for valid code, forcing the use of standard but optional extensions but in this particular case does not seem to support strerrorlen_s
. Refer to Missing C11 strerrorlen_s function under MSVC 2017 for more information.
To make matters worse, the Microsoft implementation of many of their secure functions have different semantics from the standard one documented in Annex K, which caused other systems to reject these functions and refuse to implement them. It is therefore recommended to avoid them for portability's sake.
In order to use standard functions such as fopen()
with Visual Studio, you can add these 3 lines at the beginning of your source files, before the first #include
directive:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
The example becomes:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
...
FILE *filepoint;
if ((filepoint = fopen(fileName, "r")) == NULL) {
// File could not be opened. filepoint was set to NULL
// error code was stored in errno.
// error message can be retrieved with strerror(err);
fprintf(stderr, "cannot open file '%s': %s\n",
fileName, strerror(errno));
} else {
// File was opened, filepoint can be used to read the stream.
...
fclose(filepoint);
}