www.delorie.com/djgpp/bugs/show.cgi   search  
Bug 000175

When Created: 09/22/1997 17:11:20
Against DJGPP version: 2.01
By whom: thngu4@student.cc.monash.edu.au
Abstract: istream::tellg() return a wrong position
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

Note added: 04/18/1999 10:00:48
By whom: eliz@is.elta.co.il
I cannot reproduce this with libc.a v2.02 and GCC 2.7.2.1.  So either
this problem was solved in libc.a, or it was some usage problem.

So I'm clisong this.

Solution added: 04/18/1999 10:00:09
By whom: eliz@is.elta.co.il
Upgrade to v2.02.

Fixed in version on 04/18/1999 10:00:00
By whom: eliz@is.elta.co.il



  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2010   by DJ Delorie     Updated Jul 2010