In this blog, we are going to learn how to make a file pointer and how to open and close a file using a file pointer.
File pointer
In each program where we are using file handling, we have to make a file pointer at the start of the program (before using any file operation). The file pointer will point to that file where we want to write data or from where we want to read the data. For each file which we want to use in the program, there will be a different file pointer for each of those files. Below is the syntax to declare file pointers.
FILE *<user defined name of file pointer>;
Suppose we want to make file pointer fp1 to point to a text file having the name f1.txt. Below is an example of how to make a file pointer fp1.txt.
Ex. FILE *fp1;
Opening file
If we want to perform any file operation to any file then we have to open that file using a file pointer. Then and then we can perform any file operation to that file. Below is the syntax to open the file.
<user-defined name of file pointer> = fopen(<filename with extension>,<file opening mode>;
File opening modes
File opening modes say what operation we want to do with a file. To read(reading data from file), write(write data to file, which overwrites previous data), and append(write data at the end of file, which does not overwrite data).
File operating mode | Operation performed with the file | Description |
r | Read | If the file exists then reads otherwise returns NULL |
w | Write | If the file exists then writes otherwise it will make a new file and write |
a | Append | If the file exists then appends otherwise it will make a new file and append |
r+ | Read and write | If the file exists then reads and writes otherwise it will return NULL |
w+ | Write and read | If the file exists then writes and reads otherwise it will make a new file and write, read |
a+ | Append and read | If the file exists then reads and appends otherwise it will make a new file and append, read |
Now let us take the same previous example of file pointer fp1 which points to file f1.txt. Below is the example syntax of fopen if we want to read data from file f1.txt.
fp1 = fopen(“f1.txt”,”r”);
Closing file
After opening a file, we perform many file operations using getc(), putc(), fgets(), fputs() and many more functions. After using file functions, at the end of the program we have to end the file using the fclose() function. The syntax for the same is as written below.
fclose(<file pointer of that file which we want to close>);
As a previous example, file pointer fp1 was for file f1.txt. Below is an example to close f1 file
fclose(fp1);
Example program structure to understand the use of fopen and fclose
As we have not learned file handling functions, we can not write the whole program. So below is the program structure which is as written below.
void main() { FILE *fp1; fp1 = fopen("f1.txt","r"); ................ ................ ................ fclose(fp1); }
Learning from this blog:
- What is a file pointer?
- Syntax to declare file pointer.
- How to open a file and what are file opening modes.
- How to close the file.
- The generalized structure of the FILE handling program.