Fopen giving error

void In(void) //read the image
{
int i, j;
double el;
char buff[255];
stream = fopen("test.txt", "r"); //skeltonize -> newedg.txt & normal myFile1
for (j = 0; j < ny; j++) //row
for (i = 0; i < nx; i++) //col
{
fscanf(stream, "%lf", &el);
I[i][j] = el;
}
fclose(stream);
}

This is the aspect of the code and this is the error i get:

Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.

Error C4996 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead.
closed account (E0p9LyTq)
If you are using Visual Studio you can #define _CRT_SECURE_NO_WARNINGS before your #include files.

Not recommended. It is using a bomb to swat a fly. And doesn't fix the problems with the functions, you can still have buffer overruns.

OR

Use C11's fopen_s() and fscanf_s() function that was created to prevent buffer overruns. As suggested in the error messages.

https://en.cppreference.com/w/c/io/fopen
https://en.cppreference.com/w/c/io/fopen
Still not working. Any other solution
Don't use C code. Write C++ instead. Something like this should do it, assuming I is an array of some sensible type.

1
2
3
4
5
6
7
8
ifstream inputFile("test.txt");
for (j = 0; j < ny; j++) //row
{
  for (i = 0; i < nx; i++) //col
  {
    inputFile >>  I[i][j];
  }
}
Last edited on
You should check if and why fopen fails.
1
2
3
4
5
6
stream = fopen("test.txt", "r"); 
if (stream == NULL)
{
  perror(NULL); // #include <stdio.h>
  return;
}
Topic archived. No new replies allowed.