...
| Code Block | ||
|---|---|---|
| ||
void do_stuff(void) {
FILE *logfile = fopen("log", "a");
/* Check for errors, write logs pertaining to do_stuff(), etc. */
}
int main(void)
{
FILE *logfile = fopen("log", "a"); /* Check for errors, write logs pertaining to main(), etc. */
do_stuff();
/* ... */
}
|
...
In this compliant solution, a reference to the file pointer is passed around so that the file does not have to be opened twice separatelyas an argument to functions that need to perform operations on that file. This eliminates the need to open the same file multiple times.
| Code Block | ||
|---|---|---|
| ||
void do_stuff(FILE **file) {
FILE *logfile = *file;
/* Check for errors, write logs pertaining to do_stuff, etc. */
}
int main(void) {
FILE *logfile = fopen("log", "a");
/* Check for errors, write logs pertaining to main, etc. */
do_stuff(&logfile);
/* ... */
}
|
...