× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi all
I'm trying to write a function that opens a file, does some checking and returns to the main program. The problem is that the function is located in a separate module and the main program is unaware of the file at all. All it's interested in is the result of the function. So it can't pass the file as a parameter. On the other hand I don't want the function to open and close the file on every call. For example, here is my code in the function:

_RIOFB_T *fb;
_RFILE *infile;
if ( (infile = _Ropen("FILENAME", "rr")) == NULL ) {
 print_err("Can't open FILENAME file\n");
 return 0;
}
....
// some code here
....
_Rclose(infile);
return 1;

The first solution that came to my mind is to declare infile "static" and only open it once. It actually works, but then I don't know when to close the file, which is probably okay because the file will be closed when the main program finishes. I guess it's just not the correct programming. It's actually a very small function, so I would want to create global variables and handle it with 2 more functions (one for open and another for close).
Are there any other ways?

Thanks in advance.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.