#include #include #include #include #include void tread(istream* s); void tread2(istream* s); void twrite(ostream* s); 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) -> Linux Pb \n" << " r2 : Read data file with seekg(ios::beg) 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; s->seekg(-(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)); i = 9; cout << "Seek-tread " << i << " 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)); i = 1; cout << "Seek-tread2 " << i << " 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<7; 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; 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 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg); for(i=1; i<3; i++) { pos = s->tellg(); s->read((char *)(&iv), sizeof(int)); s->read((char *)(&fv), sizeof(float)); s->read((char *)(&dv), sizeof(double)); cout << "Seek-tread2-2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << " Pos= " << pos << endl; } }