Q:

C++ program to demonstrate example of tellg() and tellp() function

0

C++ program to demonstrate example of tellg() and tellp() function

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

tellg() and tellp() example in c++

//C++ program to demonstrate example of tellg() and tellp() function.

#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
    fstream file;
    //open file sample.txt in and Write mode
    file.open("sample.txt",ios::out);
    if(!file)
    {
        cout<<"Error in creating file!!!";
        return 0;
    }
    //write A to Z
    file<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //print the position
    cout<<"Current position is: "<<file.tellp()<<endl;
    file.close();
 
    //again open file in read mode
    file.open("sample.txt",ios::in);
    if(!file)
    {
        cout<<"Error in opening file!!!";
        return 0;
    }
    cout<<"After opening file position is: "<<file.tellg()<<endl;
     
    //read characters untill end of file is not found
    char ch;
    while(!file.eof())
    {
        cout<<"At position : "<<file.tellg();   //current position
        file>>ch;   //read character from file
        cout<<" Character ""<<ch<<"""<<endl;
    }
 
    //close the file
    file.close();
    return 0;
}

Output

    Current position is: 26   
    After opening file position is: 0   
    At position : 0 Character "A"   
    At position : 1 Character "B"   
    At position : 2 Character "C"   
    At position : 3 Character "D"   
    At position : 4 Character "E"   
    At position : 5 Character "F"   
    At position : 6 Character "G"   
    At position : 7 Character "H"   
    At position : 8 Character "I"   
    At position : 9 Character "J"   
    At position : 10 Character "K"  
    At position : 11 Character "L"  
    At position : 12 Character "M"  
    At position : 13 Character "N"  
    At position : 14 Character "O"  
    At position : 15 Character "P"  
    At position : 16 Character "Q"  
    At position : 17 Character "R"  
    At position : 18 Character "S"  
    At position : 19 Character "T"  
    At position : 20 Character "U"  
    At position : 21 Character "V"  
    At position : 22 Character "W"  
    At position : 23 Character "X"  
    At position : 24 Character "Y"  
    At position : 25 Character "Z"  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to write and read time in/from binary ... >>
<< C++ program to write and read object using read an...