Hi all. First post on the forum.
I'm writing a generic funtion to open files along my program, but am worried if the pointers are being closed correctly.

The function definition:

FILE* openfile(char *FileName, char *Mode)
	{
	FILE *TempFile;
	if((TempFile = fopen(FileName, Modo)) == NULL)
		{
		fprintf(stderr, "Couldn't open file '%s'. Does it exist? Do you have permission to read it?\n", FileName);
		exit(1);
		}
	return(TempFile);
	}

Calling the function in main:

int main(int argc, char **argv)
{
/* blahblahblah */
FILE *thefile;
char *thefilename;

thefilename = argv[1];
thefile = openfile(thefilename, "r");

/* uses the file */

fclose(thefile);
return(0);
}

What I'd like to know is if fclose(thefile) is closing all references to the file opened. That's because in the function we have TempFile actually opening the file. Is TempFile's reference also being closed when we call fclose(thefile) in main?
Thanks a lot.

if fclose(thefile) is closing all references to the file opened

fclose doesn't close references. It closes a FILE object.
It doesn't matter how many pointers point a particular one. It was opened once, and it must be closed also once.

Edited 6 Years Ago by nezachem: n/a

Nezachem, thanks for the input. I thought about it but wasn't 100% sure.
Do you have any suggestion on how I could pass the global FILE* by reference?
I can't use something like:

void openfile(FILE *tempfile, char *filename, char *mode)
{
/* blahblahblah */
}

And call this way:

openfile(thefile, filename, "r");

Because it gives me a segmentation fault when the program is run, though the compilation goes ok.
Thanks in advance.

Hi there, Jephthah.
If I define the function this way so it can take a FILE** as argument:

void openfile(FILE **TempFile, char *FileName, char *Mode)
{
if((TempFile = fopen(FileName, Mode)) == NULL)
   {
   fprintf(stderr, "Error opening the file... next please.\n");
   exit(1);
   }
}

The compiler gives me this warning:
"assignment from incompatible pointer type", and points to the fopen line.
Maybe there is a better way to write it?
Thanks in advance.

because now it's a pointer so you need to dereference it when you want to make an assignment to it. line 3 becomes:

if(( *TempFile = fopen(FileName, Mode) ) == NULL)

Thanks, Jephthah. It works, but I'm saving your solution for something bigger.
I just tried declaring the file pointer outside the main() function, so FileOpen() procedure could access the pointer since it is global now. Sometimes less is more.
It is stressing to write a text parser in C, and AWK just proved to be too slow for the needed task.
Thank you very, Daniweb is a fountain of knowledge.

globals are widely considered poor programming. you should learn to not use them, by not using them unless you have a condition where it's necessary to do so.

You think "it works" for you now, but it's a Faustian bargain. And it will come back to haunt you.


.

Edited 6 Years Ago by jephthah: n/a

Hi there, Jephthah.
You are right about global variables being bad if one abuses their use without care. For this, we have the static modifier in C, which I'm using.for this task. :-)
I tend to compare this to the "don't use goto because it's not necessary". The new programmers are inflated by this, but they never touched, for example, a compiler, so they don't know that a compiler can't be written without goto, and Aho reinforces this.
That's why goto was left in C. And Dijkstra, the greatest programmer that ever existed, never intended to abolish the use of goto when he invented the structured programming 40 years ago (though he discouraged its use whenever possible).

Comments
gtfo
This question has already been answered. Start a new discussion instead.