#include #include #include #include #include void tread(istream* s); void tread2(istream* s); void twrite(ostream* s); // ----------------------------------------------------- // This test program illustrates the problem with g++ // (pb with g++ 2.95 3.0) // iostream library with seekg(..., ios::end) // # compile the program // csh> g++ -o tios tios.cc // # Create the test data file tios.dat // csh> ./tios w // # Read with seekg(... ios::end) ---> wrong result // csh> ./tios r // # seekg(... ios::beg) works OK // csh> ./tios r2 // ---------------------------------------------------- int main(int narg, char ** arg) { if (narg < 2) { cout << "Usage: tios w/r/r2 \n" << " w : Write data file tios.cc \n" << " r : Read data file with seekg(ios::end) -> g++/Linux Pb \n" << " r2 : Read data file with seekg(ios::beg) g++/Linux OK \n" << endl; exit(0); } istream* is; ostream* os; if (arg[1][0] == 'r') { // is = new ifstream("tios.dat", ios::in | ios::binary); is = new ifstream("tios.dat", ios::in ); if ( arg[1][1] == '2') tread2(is); else tread(is); delete is; } else { // os = new ofstream("tios.dat", ios::out | ios::binary); os = new ofstream("tios.dat", ios::out ); twrite(os); delete(os); } } void twrite(ostream* s) { int i; int iv; float fv; double dv; for(i=0; i<10; i++) { iv = 1000+i*50; fv = iv+2.5; dv = iv+3.141596; s->write((char const*)(&iv), sizeof(int)); s->write((char const*)(&fv), sizeof(float)); s->write((char const*)(&dv), sizeof(double)); cout << "twrite " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << endl; } } void tread(istream* s) { int i; int iv; float fv; double dv; long pos; for(i=0; i<10; i++) { pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } cout << " ----- Check Output from here ------- " << endl; for(i=0; i<4; i++) { s->seekg(-i*(sizeof(int)+sizeof(float)+sizeof(double)), ios::end); pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "Seek-tread-end " << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg); pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "Seek-tread-begin " << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } void tread2(istream* s) { int i; int iv; float fv; double dv; long pos; for(i=0; i<10; i++) { pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } cout << " ----- Check Output from here ------- " << endl; for(i=1; i<5; i+=2) { s->seekg(i*(sizeof(int)+sizeof(float)+sizeof(double)), ios::beg); pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "Seek-tread2-begin " << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } s->seekg(0, ios::end); pos = s->tellg(); cout << "Seek-tread2 s->seekg(0, ios::end); Pos = " << pos ; pos -= (sizeof(int)+sizeof(float)+sizeof(double)); cout << " ---> Pos= " << pos << endl; s->seekg(pos, ios::beg); pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); i = 9; cout << "Seek-tread2-begin " << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; }