100% found this document useful (1 vote)
877 views3 pages

File Pointers and Manipulators in C++

This document discusses file pointers in C++. It explains that C++ uses a get pointer and put pointer for reading from and writing to files respectively. It describes the seekg(), tellg(), seekp(), and tellp() functions. seekg() and seekp() are used to move the get and put pointers, while tellg() and tellp() return the positions of the get and put pointers. The reference points for seeking are the beginning, end, or current position of the file. Examples are provided to demonstrate using these functions.

Uploaded by

durgasunith22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
877 views3 pages

File Pointers and Manipulators in C++

This document discusses file pointers in C++. It explains that C++ uses a get pointer and put pointer for reading from and writing to files respectively. It describes the seekg(), tellg(), seekp(), and tellp() functions. seekg() and seekp() are used to move the get and put pointers, while tellg() and tellp() return the positions of the get and put pointers. The reference points for seeking are the beginning, end, or current position of the file. Examples are provided to demonstrate using these functions.

Uploaded by

durgasunith22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • File Pointers in C++

[Link].

com

FILE POINTERS IN C++


(FILE MANIPULATORS)
In C++ we have a get pointer and a put pointer for getting (i.e. reading)
data from a file and putting (i.e. writing) data on the file respectively.

 seekg():-
seekg() is used to move the get pointer to a desired location with respect to
a reference point.
Syntax: file_pointer.seekg (number of bytes ,Reference point);
Example: [Link](10,ios::beg);

 tellg():-
tellg() is used to know where the get pointer is in a file.
Syntax: file_pointer.tellg();
Example: int posn = [Link]();

 seekp():-
seekp() is used to move the put pointer to a desired location with respect to
a reference point.
Syntax: file_pointer.seekp(number of bytes ,Reference point);
Example: [Link](10,ios::beg);

 tellp():-
tellp() is used to know where the put pointer is in a file.
Syntax: file_pointer.tellp();
Example: int posn=[Link]();

1|Page
[Link]

The reference points are:


ios::beg – from beginning of file
ios::end – from end of file
ios::cur – from current position in the file.

In seekg() and seekp() if we put – sign in front of number of bytes then we


can move backwards.

Program:
#include<iostream>

#include<fstream>

using namespace std;

int main()

fstream fout;

char buf[200];

[Link]("[Link]",ios::trunc | ios::out);

char ch[100];

cout<<"Enter data into file = ";

[Link](ch,20,'#');

fout<<ch;

int pos;

pos = [Link]();/*..........................................*///////tellp

cout<<"Current position of pointer = "<<pos;

[Link](3,ios::beg); /*..........................................*///////seekp
//////seekp

fout<<"nkok";

2|Page
[Link]

cout<<"\nIndex location = "<<[Link]()<<endl;/*.........................*///////tellg

[Link]();

[Link]("[Link]", ios :: in | ios :: ate);

cout << "\nReading from the file ... " << endl;

[Link](3); /*..........................................*///////seekg

while (![Link]()) {

[Link](buf, 100);

cout << buf << endl;

Output:

3|Page

Ashishprajapati29.wordpress.com 
1 | 
 
P a g e
 
FILE POINTERS IN C++ 
(FILE MANIPULATORS) 
In C++ we have a get pointer and
Ashishprajapati29.wordpress.com 
2 | 
 
P a g e
 
The reference points are: 
ios::beg  – from beginning of file 
ios::end  –
Ashishprajapati29.wordpress.com 
3 | 
 
P a g e
 
 
cout<<"
Index location = "<<fout.tellg()<<endl;/*.......................

You might also like