Manipulation of file pointers
Write a C++ Program of Manipulation of file pointers in File Handling. Here’s simple Program of Manipulation of file pointers in File Handling in C++ Programming Language.
Below is the source code for C++ Program of Manipulation of file pointers in File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program of Manipulation of file pointers in File Handling */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream fp;
char buf[100];
int pos;
// open a file in write mode with 'ate' flag
fp.open("C:\\Users\\acer\\Documents\\file4.txt", ios :: out | ios :: ate);
cout << "\nWriting to a file ... " << endl;
fp << "This is a one line" << endl; // write a line to a file
fp << "This is a another line\n" << endl; // write another file
pos = fp.tellp();
cout << "\nCurrent position of put pointer : " << pos << endl;
// move the pointer 10 bytes backward from current position
fp.seekp(-10, ios :: cur);
fp << endl << "Writing at a random location ";
// move the pointer 7 bytes forward from beginning of the file
fp.seekp(7, ios :: beg);
fp << " Hello World ";
fp.close(); // file write complete
cout << "\nWriting Complete .... " << endl;
// open a file in read mode with 'ate' flag
fp.open("C:\\Users\\acer\\Documents\\file4.txt", ios :: in | ios :: ate);
cout << "\nReading from the file ... \n" << endl;
fp.seekg(0); // move the get pointer to the beginning of the file
// read all contents till the end of file
while (!fp.eof())
{
fp.getline(buf, 100);
cout << buf << endl;
}
pos = fp.tellg();
cout << "\nCurrent Position of get pointer : " << pos << endl;
return 0;
}
OUTPUT : :
/* C++ Program of Manipulation of file pointers in File Handling */ Writing to a file ... Current position of put pointer : 46 Writing Complete .... Reading from the file ... This is Hello World his is a anothe Writing at a random location Current Position of get pointer : -1 Process returned 0
Above is the source code for C++ Program of Manipulation of file pointers in File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….