The problem I have and can't understand is that I receive a segfault when trying to close the second file (fclose(ofp)).
Input file is a xml file that will generate food for output file.
At present time, input file is being read for parsing but no output for outfile is generated.
The error I get is:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7aae9c2 in _int_free () from /lib64/libc.so.6
gcc 4.5.2
The relevant code is:
FILE * openFile (char * filen_in, char * mode, unsigned short int err) {
/* Open SAFT file. */
FILE * fp;
if ((fp = fopen (filen_in, mode)) == NULL) {
error_handler(err);
}
return fp;
}
void closeFile (FILE * fp, unsigned short int err) {
/* Close output file. */
if (fclose(fp) != 0) {
error_handler(err);
}
}
int main() {
FILE *ifp;
FILE *ofp;
char * filen_in = "a.xml";
char * filen_out = "out.txt";
ifp = openFile(filen_in, "r", 1);
ofp = openFile(filen_out, "a", 3);
/* nr_chars = readSt(ifp); */
closeFile(ifp, 2);
closeFile(ofp, 4);
return 0;
}
What is going here? How can I fix this?