[1055] | 1 | #include <stdio.h>
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 | #include <math.h>
|
---|
| 4 | #include <iostream.h>
|
---|
| 5 | #include <fstream.h>
|
---|
| 6 |
|
---|
| 7 | void tread(istream* s);
|
---|
| 8 | void twrite(ostream* s);
|
---|
| 9 |
|
---|
| 10 | int main(int narg, char ** arg)
|
---|
| 11 | {
|
---|
| 12 | if (narg < 2) {
|
---|
| 13 | cout << "Usage: tios r/w" << endl;
|
---|
| 14 | exit(0);
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | istream* is;
|
---|
| 18 | ostream* os;
|
---|
| 19 | if (arg[1][0] == 'r') {
|
---|
| 20 | // is = new ifstream("tios.dat", ios::in | ios::binary);
|
---|
| 21 | is = new ifstream("tios.dat", ios::in );
|
---|
| 22 | tread(is);
|
---|
| 23 | delete is;
|
---|
| 24 | }
|
---|
| 25 | else {
|
---|
| 26 | // os = new ofstream("tios.dat", ios::out | ios::binary);
|
---|
| 27 | os = new ofstream("tios.dat", ios::out );
|
---|
| 28 | twrite(os);
|
---|
| 29 | delete(os);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | void twrite(ostream* s)
|
---|
| 35 | {
|
---|
| 36 | int i;
|
---|
| 37 | int iv;
|
---|
| 38 | float fv;
|
---|
| 39 | double dv;
|
---|
| 40 | for(i=0; i<10; i++) {
|
---|
| 41 | iv = 1000+i*50;
|
---|
| 42 | fv = iv+2.5;
|
---|
| 43 | dv = iv+3.141596;
|
---|
| 44 | s->write((char const*)(&iv), sizeof(int));
|
---|
| 45 | s->write((char const*)(&fv), sizeof(float));
|
---|
| 46 | s->write((char const*)(&dv), sizeof(double));
|
---|
| 47 | cout << "twrite " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << endl;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | void tread(istream* s)
|
---|
| 52 | {
|
---|
| 53 | int i;
|
---|
| 54 | int iv;
|
---|
| 55 | float fv;
|
---|
| 56 | double dv;
|
---|
| 57 | long pos;
|
---|
| 58 | for(i=0; i<10; i++) {
|
---|
| 59 | pos = s->tellg();
|
---|
| 60 | s->read((char *)(&iv), sizeof(int));
|
---|
| 61 | s->read((char *)(&fv), sizeof(float));
|
---|
| 62 | s->read((char *)(&dv), sizeof(double));
|
---|
| 63 | cout << "tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
|
---|
| 64 | << " Pos= " << pos << endl;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | s->seekg(-(sizeof(int)+sizeof(float)+sizeof(double)), ios::end);
|
---|
| 68 | pos = s->tellg();
|
---|
| 69 | s->read((char *)(&iv), sizeof(int));
|
---|
| 70 | s->read((char *)(&fv), sizeof(float));
|
---|
| 71 | s->read((char *)(&dv), sizeof(double));
|
---|
| 72 | i = 9;
|
---|
| 73 | cout << "Seek-tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
|
---|
| 74 | << " Pos= " << pos << endl;
|
---|
| 75 | s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg);
|
---|
| 76 | pos = s->tellg();
|
---|
| 77 | s->read((char *)(&iv), sizeof(int));
|
---|
| 78 | s->read((char *)(&fv), sizeof(float));
|
---|
| 79 | s->read((char *)(&dv), sizeof(double));
|
---|
| 80 | i = 1;
|
---|
| 81 | cout << "Seek-tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
|
---|
| 82 | << " Pos= " << pos << endl;
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|