ifstream tellg() not returning the correct position

had the same issue. try to read the filestream binary:

    ifstream myfile("savingaccount.txt",ios::binary);

it helped for me


This seems more like a compiler bug (probably gcc)

With the following Code:-

#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
    int id;
    char name[50];
    ifstream myfile("savingaccount.txt");  //open the file
    cout << myfile.tellg()<<endl;
    myfile >> id;
    streamoff pos=myfile.tellg();
    cout <<"pos= "<<pos<<'\n';
    cout <<"id= " << id<<'\n' ;
    return 0;
}

Following is the output:-

Bug

In the image inpstr.exe was generated from Visual studio's cl while inp.exe from g++(gcc version 4.6.1 (tdm-1))

Tags:

C++

File

Fstream