fclose always crash my program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//inFile : Pointer to a FILE object that specifies an input stream.
//IN_NAME : The file name
//TYPE : C string file access modes
void openFile(FILE *inFile, std::string const& IN_NAME, std::string const& MODE)
{
  inFile = fopen(IN_NAME.c_str(), MODE.c_str() );
  if(!inFile)
  {
    std::cerr<<IN_NAME<<" cannot be opened\n";
    std::cin.get();
    exit(1);
  }
}

int main()
{
 FILE* inFile = 0;
 openFile(inFile, "lena.dat", "rb");
 fclose(inFile); //this line always crash
 
 return 0;
}


What kind of mistakes do I make?
Am I change the address of the inFile?
Thanks
Last edited on
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.
Your problem is that you are doing this:
1
2
3
    main: inFile = 0
==> openFile: inFile = 0...inFile = 1
==> main: inFile = 0, fclose(inFile)


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 recommend simply using an fstream

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().


I try both of them, but the result are weird
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FILE* openFile(std::string const& IN_NAME, std::string const& MODE)
{
  FILE* inFile = fopen(IN_NAME.c_str(), "wb" );
  if(!inFile)
  {
    std::cerr<<IN_NAME<<" cannot be opened\n";
    std::cin.get();
    exit(1);
  }
  return inFile;
}

int main()
{
  FILE* inFile = openFile ("lena.dat", "rb");
  //do something
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void openFile(FILE** inFile, std::string const& IN_NAME, std::string const& MODE)
{
  *inFile = fopen(IN_NAME.c_str(), "wb" );
  if(!(*inFile))
  {
    std::cerr<<IN_NAME<<" cannot be opened\n";
    std::cin.get();
    exit(1);
  }
}

int main()
{
  FILE *inFile = 0;
  openFile(inFile, "lena.dat", "rb");
  // do something
}


The program never crash again, but looks like I can't read the data from inFile
The file which I try to open is filled with "zero"(or nothing?)

Since I don't know how to fix it, I am using another solution
1
2
3
4
5
6
7
8
9
inline void checkFile(FILE* inFile)
{
  if(!inFile)
  {
    std::cerr<<"The file cannot be opened\n";
    std::cin.get();
    exit(1);
  }
}
Last edited on
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.
chronokitsune, thanks a lot
Topic archived. No new replies allowed.