Loading....
Coupon Accepted Successfully!

 
Manipulators With Arguments
The seekp() and seekg() functions can be used with two arguments. Their formats with two arguments are as follows:
 

seekg(offset, pre_position);

seekp(offset, pre_position);

 
The first argument offset specifies the number of bytes the file pointer is to be shifted from the argument pre_position of the pointer. The offset should be a positive or negative number. The positive number moves the pointer in the forward direction, whereas the negative number moves the pointer in the backward direction. Fig 16.11 provides the status of pre-position arguments. The pre_position argument may have one of the following values:
 
  • ios::beg Beginning of the file
  • ios::cur Current position of the file pointer
  • ios::end End of the file
 
Fig: Status of pre-position arguments
In the above figure, the status of ios::beg and ios::end is shown. The status of ios::cur cannot be shown to be similar to ios::beg or ios::end. The ios::cur means the present position of the file pointer. The ios::beg and ios::end may be referred to as ios::cur. Suppose the file pointer is in the middle of the file and you want to read the file from the beginning, you can set the file pointer at the beginning using ios::beg. However, if you want to read the file from the current position, you can use the option ios::cur.
 
The seekg() function shifts the associated file’s input (get) file pointer. The seekp() function shifts the associated file’s output (put) file pointer. Table describes a few pointer offsets along with their working.

Table: File pointer with its arguments
 

Seek option

Working

in.seekg (0,ios :: beg)

Go to the beginning of file

in.seekg (0,ios :: cur)

Rest at the current position

in.seekg (0,ios ::end)

Go to the end of file

in.seekg (n,ios :: beg)

Shift file pointer to n+1 byte in the file

in.seekg (n,ios :: cur)

Go front by n byte from the current position

in.seekg (-n,ios :: cur)

Go back by n bytes from the present position.

in.seekg (-n,ios::end);

Go back by n bytes from the end of file

 
In Table, in is an object of the ifstream class.
 

16.13 Write a program to write text in the file. Read the text from the file from end of file. Display the contents of file in reverse order.

#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();

ifstream in;

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

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

in.seekg(0,ios::end);

int m=in.tellg();
char ch;
for (int i=1;i<=m;i++)
{
in.seekg(-i,ios::end);
in>>ch;
cout<<ch;
}
return 0;

}

OUTPUT
Enter text
Visual_C_+_+
Reverse Contents of the file
+_+_C_lausiV
 
Explanation: In the above program, file text is opened in the output mode, and the string entered is written to the file. Again, the same file is opened for reading purpose. The statement in.seekg (0,ios::end); moves the get pointer at the end of the file. The tellg() function returns the current position of the file pointer in the file. Hence, the file pointer is set to the end of the file. The tellg() returns the number of last bytes, that is, the size of the file in bytes, and it is stored in the integer variable m. The for loop executes from 1 to m. The statement in.seekg (-i, ios::end) reads the ith byte from the end of the file. The statement in>>ch reads the character from the file indicated by the file pointer. The cout statement displays the read character on the screen. Thus, the contents of the file are displayed in reverse order.
 

16.14 Write a program to enter a text and again enter a text and replace the first word of the first text with the second text. Display the contents of the 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.seekp(0,ios::beg);

cout<<“\nEnter text to replace the first word of first text:”;
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()!=1)
{ in>>data;
cout<<data;
}
return 0;

}

OUTPUT
Enter text
Visual C++
Enter text to replace the first word of first text : Turbo-
Contents of the file
Turbo-C++
 
Explanation: In the above program, the text is entered and written in the file text. This process is explained in the previous examples. Here again, the statement out.seekp (0,ios::beg); sets the file pointer (put pointer) at the beginning of the file. Again, text is entered and written at the current file pointer position. The previous text is overwritten.




Test Your Skills Now!
Take a Quiz now
Reviewer Name