Loading....
Coupon Accepted Successfully!

 

File Pointers And Manipulators

All file objects hold two file pointers that are associated with the file. These two file pointers provide two integer values. These integer values indicate the exact position of the file pointers in the number of bytes in the file. The read or write operations are carried out at the location pointed by these file pointers .One of them is called get pointer (input pointer), and the second one is called put pointer (output pointer). During reading and writing operations with files, these file pointers are shifted from one location to another in the file. The (input) get pointer helps in reading the file from the given location, and the output pointer helps in writing data in the file at the specified location. When read and write operations are carried out, the respective pointer is moved.
 
While a file is opened for the reading or writing operation, the respective file pointer input or output is by default set at the beginning of the file. This makes it possible to perform the reading or writing operation from the beginning of the file. The programmer need not explicitly set the file pointers at the beginning of files. To explicitly set the file pointer at the specified position, the file stream classes provides the following functions:
 
Read mode: When a file is opened in read mode, the get pointer is set at the beginning of the file, as shown in Figure. Hence, it is possible to read the file from the first character of the file.
 
Fig: Status of get pointer in read mode
 
Write Mode: When a file is opened in write mode, the put pointer is set at the beginning of the file, as shown in Figure. Thus, it allows the write operation from the beginning of the file. In case the specified file already exists, its contents will be deleted.
 
Fig: Status of put pointer in write mode
 
Append Mode: This mode allows the addition of data at the end of the file. When the file is opened in append mode, the output pointer is set at the end of the file, as shown in Figure. Hence, it is possible to write data at the end of the file. In case the specified file already exists, a new file is created, and the output is set at the beginning of the file. When a pre-existing file is successfully opened in append mode, its contents remain safe and new data are appended at the end of the file.
 
Fig: Status of put pointer in append mode
C++ has four functions for the setting of points during file operation. The position of the curser in the file can be changed using these functions. These functions are described in Table.

Table: File pointer handling functions
 

Function

Uses

Remark

seekg()

Shifts input ( get ) pointer to a given location.

Member of ifstream class

seekp()

Shifts output (put) pointer to a given location.

Member of ofstream class

tellg()

Provides the present position of the input pointer.

Member of ifstream class

tellp()

Provides the present position of the output pointer.

Member of ofstream class

 
As given in Table, the seekg() and tellg() are member functions of the ifstream class. All the above four functions are present in the class fstream. The class fstream is derived from ifstream and ofstream classes. Hence, this class supports both input and output modes, as shown in Figure. The seekp() and tellp() work with the put pointer, and tellg() and seekg() work with the get pointer.
 
​​
Fig: Derivation of fstream class
 
Now consider the following examples:
 

16.11 Write a program to append a file.

#include<fstream.h>

#include<conio.h>

int main()

{

clrscr();

ofstream out;

char data[25];

out.open (“text”,ios::out);

cout<<“\n Enter text”<<endl;

cin.getline(data,25);

out <<data;

out.close();

out.open (“text”, ios::app );

cout<<“\n Again Enter text”<<endl;

cin.getline (data,25);

out<<data;

out.close();

ifstream in;

in.open(“text”, ios::in);

cout<<endl<<“Contents of the file \n”;

while (in.eof()==0)

{

in>>data;

cout<<data;

}
return 0;

}

OUTPUT
Enter text
C-PLUS-
Again Enter text
PLUS
Contents of the file
C-PLUS-PLUS
 
Explanation: In the above program the file text is opened for writing, that is, output. The text read through the keyboard is written in the file. The close() function closes the file. Once more, the same file is opened in the append mode, and data entered through the keyboard are appended at the end of the file, that is, after the previous text. The append mode allows the programmer to write data at the end of the file. The close() function closes the file. The same file is opened using the object of the ifstream class for reading purpose. The while loop is executed until the end of the file is detected. The statements within the while loop read text from the file and display it on the screen.
 

16.12 Write a program to read contents of the file. Display the position of the get pointer.

#include<fstream.h>

#include<conio.h>

int main()

{

clrscr();

ofstream out;

char data[32];

out.open (“text”,ios::out);

cout<<“\n Enter text”<<endl;

cin.getline(data,32);

out <<data;

out.close();

ifstream in;

in.open(“text”, ios::in);

cout<<endl<<“Contents of the file \n”;

int r;

while (in.eof()==0)

{

in>>data;

cout<<data;

r=in.tellg();
cout<<“ (“<<r <<“)”;
}
return 0;

}

OUTPUT
Enter text
Programming In ANSI and TURBO-C
Contents of the file
Programming (11)In (14)ANSI (19)and (23)TURBO-C (31)
 
Explanation: The above program is similar to the previous one. In addition here, the function tellg() is used. This function returns the current file pointer position in the number of bytes from the beginning of the file. The number shown in brackets in the output specifies the position of the file pointer from the beginning of the file. The same program is illustrated below using the binary mode.




Test Your Skills Now!
Take a Quiz now
Reviewer Name