On line 19, inFile is still NULL. You could make openFile() take a reference to a FILE* if you want, but since it seems are are using C++ anyway I would recommend simply using an fstream.
inFile is passed in as a pointer to a FILE structure. fopen() returns a NEW pointer to a FILE structure, erasing the original pointer. That means even if you use the same variable name, you are referring to a different storage location. In other words, when you are in main(), inFile is still 0, which is why fclose() is giving you a problem. You have two alternatives:
1. Make the first parameter of openFile() be a pointer to a FILE pointer (FILE **), and use openFile(&inFile, filename, open_mode) to call openFile() with an existing FILE pointer.
2. Return a FILE pointer from openFile() and remove the first parameter instead of making it return nothing and use FILE* inFile = openFile ("lena.dat", "rb") to call openFile().
I would like to, but I am practicing the C style language since I have to work with it many times
Besides, <fstream> could be slower than <stdio.h> in many situations.
Since speed is an important factor of image processing, I would prefer <stdio.h> rather than <fstream>
1. Make the first parameter of openFile() be a pointer to a FILE pointer (FILE **), and use openFile(&inFile, filename, open_mode) to call openFile() with an existing FILE pointer.
2. Return a FILE pointer from openFile() and remove the first parameter instead of making it return nothing and use FILE* inFile = openFile ("lena.dat", "rb") to call openFile().
Your second example still only passes a FILE* argument rather than a FILE** argument. Passing `&inFile' instead of just `inFile' in main() should fix that.
As for your first example, you are opening the file for writing using "wb" rather than using the MODE parameter that you specified. That is why you can't read the file in that case.