Sathish kumar
Sathish kumar

Reputation: 49

What is tellg() in file handling in c++ and how does it work?

I tried to access the next characters to be read from the file using tellg() and returns the position correctly if the file has one line of text.. But when the file has more than one line it gives me some abnormal values.. I am attaching my code and the output i got below..

   #include <iostream>
   #include <fstream>

   using namespace std;

   int main()
   {
       char temp;
       ifstream ifile("C:\\Users\\admin\\Desktop\\hello.txt");
       ifile>>noskipws;
       while(ifile>>temp)
       {
           cout<<temp<<" "<<ifile.tellg()<<endl;
       }
   }

   output:
      H 3
      e 4
      l 5
      l 6
      o 7

        8
      W 9
      o 10
      r 11
      l 12
      d 13
      . 14
      . 15

        16
      ! 17
      ! 18
      ! 19

    File : Hello.txt contains 3 lines as given below..

           Hello
           World
           !!!

Don't understand why it starts with 3 in the print statement which should instead start from 1.. when there are 2 lines it starts printing from 2.. can anyone explain me..?

Upvotes: 2

Views: 1681

Answers (1)

IFaz
IFaz

Reputation: 36

As a matter of fact tellg() is not to return the offset of a byte in a stream, but a pos_type descriptor which is reusable by seekg(). It will match the byte offset if the file is binary, but it is not guaranteed in a text stream. (In a *ix it will match too, but in Windows there is no direct assignment.)

Open the file in binary mode, because seekg() is used with an offset. If the modification of the file happens between two runs of your program, you'll need to store positionEof in a file.

Note: In binary mode you could actually store positionEof in an integer, but I prefer using the explicite type as long as it is possible.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    streampos positionEof;

    // Record original eof position.
    ifstream instream("C:\\Users\\istvan\\Desktop\\hello.txt", ios::in | ios::binary);
    if (instream.is_open()) {
        instream.seekg(0, ios::end);
        positionEof = instream.tellg();     // store the end-of-file position
        instream.close();
    }
    else
        cout << "Record eof position: file open error" << endl;

    // Append something to the file to simulate the modification.
    ofstream outstream("C:\\Users\\istvan\\Desktop\\hello.txt", ios::app);
    if (outstream.is_open()) {
        cout << "write" << endl;
        outstream << "appended text";
        outstream.close();
    }

    // Check what was appended.
    instream.open("C:\\Users\\istvan\\Desktop\\hello.txt", ios::in | ios::binary);
    if (instream.is_open()) {
        instream.seekg(positionEof);    // Set the read position to the previous eof
        char c;
        while ( instream.get(c))
            cout << c;

        instream.close();
    }
    else
        cout << "Check modification: file open error!" << endl;

    return 0;
}

Upvotes: 2

Related Questions