File Pointer in C

September 30, 2014 0 Comments

In order to use files it is mandatory to learn the file Input / Output operations. Learning of these is nothing but to understand the functions to write data into a file, to read data from a file, etc. For at any of such operations the file must be opened. The file opening operation is done through a pointer variable called as file pointer. So, file pointer is nothing but a variable which is declared and then I to any required physical file. All the I/O operations are done to physical file through the logical file.

A File Pointer is a special type of pointer variable of the type FILE that is used to store the address of a physical file.

FILE I/O is almost identical to the console and terminal I/O operations. The pnntf() function is used to display the data on the screen. And in file handling we use fprintf() to write the data into the file. We use scanf() function to read the data from the keyboard. And in file handling we use fscanf() to read the data from the file. This is called file manipulation.

The major difference between manipulation of file and terminal I/O is that the terminal I/O is standard no need to specify anything but it is necessary to specify files to be used in the programs where file I/O are performed. It is possible have many files on the disk. If you wish to use a file or files in the programs, then you must specify which file or files you wish to use. Specification of the file that is to be used is referred as opening a file.
At the time of opening the file it is also compulsory to specify the type of operation to be performed i.e. Read from the file, Write to the file, or both. This specification is done through the file opening mode. When different files are used in the program, it is required to specify a particular file for reading or writing. It can be done using a variable called a FILE pointer.

Every file you open has its own FILE pointer variable. When you wish to write to a fiie you specify the file by using its file pointer variable. The file pointer declared using FILE data type is called as logical file. The data is not stored permanently on the secondary storage with this name. It is stored with other file name which is called as physical file name. The logical file is linked with this physical file name. The read/write operations are done on logical file in the program which is indirectly transferred from or to the physical file. The linking of logical file with physical file is done through fopen() function. The FILE pointer variables can be declared as follows:

FILE *fP1, *fp2, *fp3;

The variables fpl, fp2, fp3 are the pointer variables of the type FILE or simply file pointers. They can store the address of any physical file.

Any name can be used as file pointer but must be a valid variable or identifier name of C language. The rules of standard data type identifiers apply to these variables also. The declared FILE Pointer variable may hold address of any physical file but only one at a time. So, at a given point of time logically a logical file points to one and only one physical file. The read / write operactions performed on the file pointer indirectly directed to physical file. At the time of opening file the file pointed by file pointer variable is mentioned along with the type of operation. The opened file is closed when the operation is finished or whenever a new file is to be attached. We will discuss file opening and closing in more details in the next article.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google