Advertisement
File Pointer | Description |
---|---|
get | It automatically points at the beginning of file, allowing us to read the file from the beginning. |
File I/O Stream Classes | Description |
---|---|
ifstream | This class is used to create an input stream and gives access to get pointer, to perform the file read operation. |
fstream | This class is used to used to create an input-output stream and gives access to both get and put pointers, to perform binary file read/write operations. |
file-stream-object("filename", mode);
Advertisement
File Mode | Description |
---|---|
ios::in | Searches for the file and opens it in read mode only and gives access to get pointer, to read the content of a file. |
Functions to use get pointer | Description |
---|---|
tellg() | Gives us the current location of the get pointer. When the file is opened in a read-only mode, tellg() returns zero i.e. the beginning of the file. |
seekg (streampos position ); | This function is used to set the location of the get pointer to a desired position/offset. The position variable is the new position in the file i.e. an integer value representing the number of bytes from the beginning of the file. |
seekg ( streamoff offset, ios_base::seekdir dir ); | This function is used to set the location of the get pointer to a desired position/offset. The position variable is the new position in the file i.e. an integer value representing the number of bytes from the beginning of the file. The position variable is relative to the dir parameter. dir is the seeking direction and it can take any of the following constant values: ios::beg - offset from the beginning of the file. ios::cur - offset from the current position in the file. ios::end - offset from the end of the file. |
10
a
9.99
Hello World!
Keep Smiling!
//Modifying the data to a file using fstream class and modes ios::
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//Creating an input stream to read the content of a file
ifstream ifstream_ob;
//Opening a file named country1.txt to read its content
ifstream_ob.open("File1.txt", ios::in);
cout<<"The first location in the file : " <<ifstream_ob.tellg() << "\n";
char ch;
cout<<"\nReading the content of file : \n";
//Read the file until EOF is reached
while(ifstream_ob)
{
ch = ifstream_ob.get();
cout<<ch;
}
//Setting the EOF flag off, to allow the access of file again for reading
ifstream_ob.clear();
cout<<"\n\nReading the content of file once again : \n";
//Taking the get pointer at the zero byte location from the beginning of the file
ifstream_ob.seekg(0, ios::beg);
//Reading the content of the file again
while(ifstream_ob)
{
ch = ifstream_ob.get();
cout<<ch;
}
return 0;
}
The first location in the file : 0
Reading the content of file :
10
a
9.99
Hello World!
Keep Smiling!
Reading the content of file once again :
10
a
9.99
Hello World!
Keep Smiling!
ifstream_ob.seekg(0, ios::beg);
ofstream_ob.clear();
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement