#include #include #include #include #include void tread(istream* s); void twrite(ostream* s); int main(int narg, char ** arg) { if (narg < 2) { cout << "Usage: tios r/w" << 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 ); 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; } 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; }