I found that istream::tellg() reports the wrong read position for the first call
unless it is called as early as the file was opened and no reading was done.
Following is the code that produce the error which I compiled with DJG-compiler
version 2.7.2.1 on DOS. The code is suppose to saved in the file 'file.cpp'
/*******************/
#include <iostream.h>
#include <fstream.h>
ifstream ifile;
char buffer[128];
void tellg1(void)
{
int i;
long pos;
ifile.open("file.cpp", ios::in);
// read something
for (i=1 ; i <= 10 ; i++)
{
ifile.getline(buffer, 128);
cout << i << ": " << buffer << endl;
}
for ( ; i <=15 ; i++)
{
pos = ifile.tellg(); // tellg() reports the wrong file position
ifile.getline(buffer, 128);
cout << i << ": - pos: " << pos << " ->" << buffer << endl;
}
ifile.close();
}
void tellg2(void)
{
int i;
long pos;
ifile.open("file.cpp", ios::in);
ifile.tellg(); // work around call tellg() as soon as the file was opened
for (i=1 ; i <= 10 ; i++)
{
ifile.getline(buffer, 128);
cout << i << ": " << buffer << endl;
}
for ( ; i <= 15 ; i++)
{
pos = ifile.tellg(); // now tellg() report the right position
ifile.getline(buffer, 128);
cout << i << ": - pos: " << pos << " ->" << buffer << endl;
}
ifile.close();
}
void main(void)
{
cout << "tellg() bug" << endl;
tellg1();
cout << endl << endl << "tellg() work-around" << endl;
tellg2();
}
/****************/
I tried to compile and run this piece of code on a UNIX, and the problem not
happen at all. Therefore I don't know is that a compiler bug, a library bug or
operating system bug.
Can you please check this out for me, and give me the answer about the error
Thank you very much
Trung Nguyen