In your calling code, you have:
FILE *fp = 0;
char game[SIZE];
Given that you're opening the file in the function, but not closing it, you need to change the file in the function, so the call could be:
playGame(game, &fp);
if (fp != 0)
fclose(fp);
But the function should be changed to:
void playGame(char array1[SIZE], FILE **aPtr)
{
FILE *fp = fopen("words.txt", "r");
if (fp != 0)
{
*aPtr = fp;
...other code...
}
}
Alternatively, the file should be opened and closed in the calling code and the file pointer simply passed to playGame()
:
FILE *fp = fopen("words.txt", "r");
if (fp != 0)
{
playGame(game, fp);
fclose(fp);
}
With this interface, there is no fopen()
(or fclose()
) call in playGame()
, which keeps its current interface (void playGame(char game[SIZE], FILE *fp)
). This makes more sense. Very often it is correct for the function that opens a file to be responsible for closing it too. There are exceptions, but not all that many of them.