How do you know the file is opened correctly?
- Put the perror() line in the bottom end of the function to ensure that it tells you if it fails to do the open.
Beacuse I put the perror in the if sentence and then it tells me that there is no error.
Put printf() in, like suggested before, except, don't do this:
>printf ("The read language file is %d\n", file.LanguageFile);
because if will fail. You'll need something like this
>printf ("The read language file is %s\n", file.LanguageFile);
In the if sentence I get the result. But when I was calling the function the result was an empty string. And of course, I used the second sentence to do that.
And finally I found the bug. The bug was the "return file.LanguageFile" sentence. Here's the code that fixes the bug .
Code:
char *ReadLanguage () // function reads a language file from the file "setup.cfg"
{
FILE *fin = NULL;
Setup file;
char *Text = NULL, *Temp = NULL;
if ((fin = fopen (STPFILE, "rb")) != NULL)
{
fread (&file, sizeof (Setup), 1, fin);
fclose (fin);
Text = malloc (sizeof (char) * 30);
if (Text == NULL) exit (1); // this is just temporary
Temp = Text;
strncpy (Temp, file.LanguageFile, strlen (file.LanguageFile) + 1);
return Text;
}
else return NULL;
}