File Pointers and Stream Manipulation in C++
User-defined file pointers and stream manipulation functions in the C++ programming language provide C++ users with greater control over file input and output operations, particularly when C++ users need to navigate, modify, or format data within existing text and binary files.

File pointers are used in C++ program file handling to manage the current location of an existing file while reading or writing to it.
Stream manipulator features in C++ programs help C++ users control and manage the format (e.g., precision, alignment, space filling) of file input and output.
File Pointers Concept in C++.
When C++ users are dealing with a file streams (ifstream, ofstream, fstream) class library in file handling. So, the file pointer (also known as the file cursor) by default indicates the current location for reading or writing data and information within the current file.
For file input operations tasks in file handling, the file pointer is positioned at the beginning of the file.
For file output operations tasks in file handling, the file pointer is positioned at the end (if C++ users are adding data and information), or at the beginning (if C++ users are overwriting the file).
Common file handling file pointer operation types.
- seekg – Moves the file pointer for input file operation tasks (get) in file handling.
- seekp – Moves the file pointer for output file operation tasks (put) in file handling.
- tellg – Indicates the current location of the file pointer for input stream files in file handling.
- tellp – Indicates the current location of the file pointer for output streams in file handling.
Moving the file pointer with the seekg() and seekp() functions in file handling.
These functions help C++ users move the file pointer to a specific location in file handling. Both seekg() and seekp() take input arguments.
Offset is the number of bytes to move the file pointer to in file handling.
Direction this can be ios::beg (beginning), ios::cur (current file location), or ios::end (end file location) indicating a space.
Syntax of seekg() and seekp() in C++ file handling.
streampos seekg (streampos offset, ios_base::seekdir dir);
streampos seekp (streampos offset, ios_base::seekdir dir);
Example of moving the file pointer with seekg().
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here we open the file in read mode for operation
ifstream inFile(“samplefile.txt”);
if (!inFile) {
cout << “Display Error during samplefile text file opening.” <<endl;
return 1;
}
// here it moves file pointer to the 9th byte position from the beginning
inFile.seekg(9, ios::beg);
// hear it read the rest of the file starting from the new position location
string textline;
getline(inFile, textline); // here it will read from the 10th byte location
cout << “Here it Read from file after seeking position. ” << textline << endl;
inFile.close();
return 0;
}
Example of moving the file pointer with seekg().
- In this example, inFile.seekg(9, ios::beg) moves the file pointer to the 9th byte location from the start of the file.
- After that, your program reads text information from that location.
Example of moving the file pointer (writing to a file) with seekp() in file handling.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here it opens the file in both input and output processing mode
fstream file(“samplefile.txt”, ios::in | ios::out);
if (!file) {
cout << “Display error during file opening.” << endl;
return 1;
}
// Here it moves the file pointer to the 7th byte from the beginning for writing purposes
file.seekp(7, ios::beg);
// Here it writes at the new position location
file << “Put Desire Text Information”;
file.close();
return 0;
}
Explanation of moving the file pointer (writing to a file) with seekp().
- Here in this example, the file.seekp(7, ios::beg) method moves the output file pointer to the 7th byte from the start.
- Then, it writes the “Put Desire Text Information” text information at that position, possibly overwriting the existing file contents.
The concept of tellg() and tellp() in C++ file handling.
These functions tellg() and tellp() indicate the current space location of the file pointer in the program.
tellg() and tellp() syntax in C++.
streampos tellg();
streampos tellp();
Here, in this syntax, the tellg() function indicates the space location of the file input pointer (ifstream/fstream).
The function tellp() indicates the space location of the output pointer (ofstream/fstream).
Example of getting the location of the current file pointer in a C++ program.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// here it opens the file in read mode for processing purposes
ifstream inFile(“samplefile.txt”);
if (!inFile) {
cout << “Display error during file opening.” << endl;
return 1;
}
// Here it reads some data from the above file
char charr;
inFile.get(chartr); // Here it reads one character from the file
// Here it gets the current position in the above file
streampos pos = inFile.tellg();
cout << “Here is the current file pointer location” << pos << endl;
inFile.close();
return 0;
}
Explanation of the concept of tellg() and tellp() in C++ file handling.
- In this example, after reading a character in the current file with inFile.get(chartr), the tellg() function returns the current position of the file pointer in bytes.

