0

In main I create a file pointer, pass that to a function to open it and I want to have a separate function to close the file but I am not able to close the file. What am I missing?

I do not get an error but the way that I am checking indicates the files did not close.

thanks for help on this.

 int _tmain(int argc, _TCHAR* argv[])
{
    FILE *inFile, *outFile;
    FileOpen(&inFile, &outFile);
    FileClose(&inFile, &outFile);

    getch();
    return 0;
}

void FileOpen(FILE **inFile, FILE **outFile)
{
//--Open InFile--------------------------------
    if ((*inFile = fopen("a.txt","r")) == NULL){
        printf("\nError Opening File. ");
        exit(0);
    }
    else {
        printf("File Opened\n");
         }

//--Open OutFile--------------------------------
    if ((*outFile = fopen("b.txt","w")) == NULL){
        printf("\nError Opening File. ");
        exit(0);
    }
    else {
        printf("File Opened\n");
         }
}


void FileClose(FILE **inFile, FILE **outFile)
{
//--Close InFile----------
    if (fclose(*inFile)){
        printf("File Closed\n");
    }
    else {
         printf("\nError Closing InFile File. ");
    }

//--Close outFile---------
    if (fclose(*outFile)){
        printf("File Closed\n");

    }
    else {
         printf("\nError Closing OutFile File. ");
    }
 }
dmaelect
  • 151
  • 2
  • 14

1 Answers1

6

You are misunderstanding the return value: fclose returns 0 on success, so you need to check if (fclose(*inFile) == 0).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • "so you need to check if (fclose(*inFile) == 0)" - But what to do if it fails? –  Mar 24 '17 at 23:55
  • @NeilButterworth: Use the `else` block? – Kerrek SB Mar 24 '17 at 23:56
  • I meant if fclose() fails, what are your alternatives? I's also be interested to know if anyone has ever had an fclose() failure, at least since the days of floppy disks. –  Mar 24 '17 at 23:58
  • 2
    @NeilButterworth: That's one of those hard questions. There's no one best answer, and it depends on your use case: if the data was optional (like a temporary log), you could ignore the error. If the data was critical, you could `abort()` your process (so higher-level protocols would retry, perhaps elsewhere). Or you could have in-process handling logic (e.g. ask the user). Files are not quite resources in the way memory or mutexes are, you can't "release" them unconditionally. An error indicates a problem with the outside world, and your answer depends on how you relate to the outside world. – Kerrek SB Mar 25 '17 at 00:03
  • @NeilButterworth: I like Kerrek's answer very much, but it is for advanced programmers who need to take a systemic approach to error handling. Less experienced programmers should log the error and continue. Why? Because in most cases when an fclose() fails, it's because the code made a mistake such as not opening the file correctly. Most programmers who try to recover, log the error, then try again, and log the error and try again,... making things worse. If it is really is a fatal error, the system architect would already worked out what to do, and the system will recover without your help. – ScottK Mar 25 '17 at 00:16