When I/O operations on a file are complete, we must close the file using the fclose function. The prototype of this function is as follows:
int fclose ( FILE *fP ) ;
The argument fp is a pointer to the file to be closed. This pointer is initialized when the file is opened. The function returns a zero value when a file is closed successfully; otherwise, it returns EOF.
When fclose is called with a file opened for an output or update operation, the contents of the file buffer are flushed, i. e., they are written to the file, the buffer is de-allocated and the file is closed. On the other hand, when fclose is called with a file opened for buffered input, the buffer is de-allocated (its contents are discarded) and the file is closed.
Example of Opening and closing a file
The program segment given below illustrates how a file specified by the user is opened as a text file for input and closed after the input operations are over.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char fname[50];
clrscr();
/* read name of the file to be opened */
printf(“Enter input file name: “);
gets(fname);
/* open the specified file */
fp = fopen(fname, “rt”);
if (fp == NULL) {
printf(“Error: Unable to open file %s\n”, fname);
exit(1);
}
/* perform input operations on given file */
fclose(fp) ; /* close the file */
return 0;
}
The variable fp is a pointer to a file structure and the character array fname is used to store the file name. The program first reads the name of the file to be opened using the gets function.
The fopen function is then used to open the specified file in read mode and the return value (a file pointer) is assigned to variable fp.If the file opening operation was unsuccessful ( fp is NULL), the program displays an error message and exits with error code 1. Otherwise, program execution continues and we can perform the desired input operations on the specified file using the file pointer fp. Finally, when the input operations are over, the file is closed using fclose. Note that we can rewrite the code for opening a file in a concise way by calling fopen from within the condition in the if statement as shown below.
if ((fp = fopen(“STUD.DAT”, “rt”)) == NULL) {
printf(“Error: Unable to open file STUD.DAT\n”);
exit(1);
Note that since the assignment operator has lower precedence than the equality operator, the fopen function call is enclosed within a pair of parentheses. Thus, the value returned by the fopen function is first assigned to the variable fp and then compared with NULL. Without the parentheses, the value returned by fopen will first be compared with NULL and then the outcome of this comparison will be assigned to variable fp, which is incorrect.
Example of Function to open a file
In above Example, observe that the code to read a file name from the keyboard and open the file spans several lines. Since almost every program involving file processing will require one or more files to be opened, it is a good idea to write a utility functions to perform this operation. Such a function will also enable us to keep the code in subsequent example programs concise. We will generalize this function to accept the file name from either the calling function or the keyboard.
The fileopen function given below has three parameters: fname, mode and msg. The parameter fname usually specifies the name of the file to be opened. However, if message string msg is not a NULL string, the function displays msg and reads a file name from the keyboard. The specified file is then opened in the given mode using fopen. If the file is opened successfully, the function returns the file pointer; otherwise, it displays an error message and exits the program.
/* open a file in specified mode */
FILE *fileopen(char *fname, const char *mode, const char *msg)
{
FILE *fp;
/* if msg is not a NULL string, read file name from keyboard */
if (strlen (msg) > 0) {
printf(“%s”, msg);
scanf(“%s”, fname);
}
/* open the file */
if ((fp = fopen(fname, mode)) ==NULL) {
printf(“Error: Unable to open file %s\n”, fname);
exit(1);
}
return fp;
}
To open a text file called marks .dat in read mode, we can call this function as shown below.
fp = fileopen(“marks.dat”, “rt”,””);
On the other hand, to read the file name from the keyboard and open that file as a text file in read mode, we can call this function as follows:
char fname[50];
fp = fileopen(fname, “rt”, “Enter input file name: “);
Note that we should copy this function in each program that uses it. This can be tedious, as we may have to add the prototype of this function at the beginning of the program. Alternatively, we can save this function in a text file, say fileopen. c, along with the required #include statements given below.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Now we can include this file in each program as shown below.
#include “fileopen.c”
Observe that the filename fileopen. c has been enclosed in double quotes, the convention to be followed for including user-defined files in a C program. We will use this approach in subsequent programs that require this function.