C File I/O Operations : fopen() and fclose()

fopen() Function :

The fopen() function is used to create a new file or open an existing file. The prototype of fopen() function call is as follows :
 FILE *fopen(const char * filename, const char * mode);
Where :
  • FILE is a datatype defined in the C standard library I/O function definitions. Therefore, all files should be declared as type FILE before they are used.
  • filename is name of the file to be opened.
  • mode specifies the purpose of opening the file. 

At here the mode can be one of the following types :

Mode Description
r Opens a text file in reading mode
w Opens or create a text file in writing mode.
a Opens a text file in append mode
r+ Opens a text file in both reading and writing mode
w+ Opens a text file in both reading and writing mode
a+ Opens a text file in both reading and writing mode
rb Opens a binary file in reading mode
wb Opens or create a binary file in writing mode
ab Opens a binary file in append mode
rb+ Opens a binary file in both reading and writing mode
wb+ Opens a binary file in both reading and writing mode
ab+ Opens a binary file in both reading and writing mode

Also note that both the filename and mode are specified as strings. They should be enclosed in quotation marks. So the general format of declaring and opening a file is :
 FILE *fp;
 fp = fopen("filename", "mode");
For example :
 FILE *fp;
 fp = fopen("myfile", "r");
The above code open a file named myfile in read only mode ("r") and returned the pointer for opened file to fp. Similarly
 FILE *fp;
 fp = fopen("myfile", "w");
This open myfile in write mode ("w").

fclose() Function :

The fclose() function is used to an open file. In C I/O operations a file must be closed as soon as all operations on it have been completed. This ensures that all the information associated with the file is flushed out from the buffers and all links to the file are broken. It also prevents any accidental misuse of the file.

The general format of fclose() function is as follows :
 fclose(file_pointer);
Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h . For example :
 FILE *fp;
 fp = fopen("myfile", "w");
 ..........
 ..........
 fclose(fp);
Now lets see some example C Programs  :

Example 1 :
#include <stdio.h>

int main() {
  FILE *fp;
  char ch;
  fp = fopen("myfile.txt", "r");
  while((ch=getc(fp)) != EOF) {
    printf("%c", ch);
  }

  fclose(fp);
  return 0;
}
Output :

Hello world. This is Sample File.

The above program opens a file called myfile.txt and prints contents by using the getc() function.

Example 2 :
#include <stdio.h>

int main() {
  FILE *fp;
  char ch;
  char str[] = "This code is written by the program.\n";

  fp = fopen("myfile.txt", "w");
  fputs(str, fp);
  fclose(fp);


  fp = fopen("myfile.txt", "r");
  while((ch=getc(fp)) != EOF) {
    printf("%c", ch);
  }
  fclose(fp);

  return 0;
}
Output :

This code is written by the program

The above code first open myfile.txt, write the string str, close the file, reopen it again and prints its content. At above the fpets() and fgets() functions are used.

Topics :