| 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 tread2(istream* s); | 
|---|
| 9 | void twrite(ostream* s); | 
|---|
| 10 |  | 
|---|
| 11 | int main(int narg, char ** arg) | 
|---|
| 12 | { | 
|---|
| 13 | if (narg < 2) { | 
|---|
| 14 | cout << "Usage: tios w/r/r2 \n" | 
|---|
| 15 | << "  w  : Write data file tios.cc \n" | 
|---|
| 16 | << "  r  : Read data file with seekg(ios::end)  -> Linux Pb \n" | 
|---|
| 17 | << "  r2 : Read  data file with seekg(ios::beg) Linux OK (??) \n" << endl; | 
|---|
| 18 | exit(0); | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | istream* is; | 
|---|
| 22 | ostream* os; | 
|---|
| 23 | if (arg[1][0] == 'r') { | 
|---|
| 24 | // is = new ifstream("tios.dat", ios::in | ios::binary); | 
|---|
| 25 | is = new ifstream("tios.dat", ios::in ); | 
|---|
| 26 | if ( arg[1][1] == '2') tread2(is); | 
|---|
| 27 | else tread(is); | 
|---|
| 28 | delete is; | 
|---|
| 29 | } | 
|---|
| 30 | else { | 
|---|
| 31 | // os = new ofstream("tios.dat", ios::out | ios::binary); | 
|---|
| 32 | os = new ofstream("tios.dat", ios::out ); | 
|---|
| 33 | twrite(os); | 
|---|
| 34 | delete(os); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | void twrite(ostream* s) | 
|---|
| 40 | { | 
|---|
| 41 | int i; | 
|---|
| 42 | int iv; | 
|---|
| 43 | float fv; | 
|---|
| 44 | double dv; | 
|---|
| 45 | for(i=0; i<10; i++) { | 
|---|
| 46 | iv = 1000+i*50; | 
|---|
| 47 | fv = iv+2.5; | 
|---|
| 48 | dv = iv+3.141596; | 
|---|
| 49 | s->write((char const*)(&iv), sizeof(int)); | 
|---|
| 50 | s->write((char const*)(&fv), sizeof(float)); | 
|---|
| 51 | s->write((char const*)(&dv), sizeof(double)); | 
|---|
| 52 | cout << "twrite " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << endl; | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void tread(istream* s) | 
|---|
| 57 | { | 
|---|
| 58 | int i; | 
|---|
| 59 | int iv; | 
|---|
| 60 | float fv; | 
|---|
| 61 | double dv; | 
|---|
| 62 | long pos; | 
|---|
| 63 | for(i=0; i<10; i++) { | 
|---|
| 64 | pos = s->tellg(); | 
|---|
| 65 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 66 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 67 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 68 | cout << "tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv | 
|---|
| 69 | << " Pos= " << pos << endl; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | cout << " ----- Check Output from here ------- " << endl; | 
|---|
| 73 | s->seekg(-(sizeof(int)+sizeof(float)+sizeof(double)), ios::end); | 
|---|
| 74 | pos = s->tellg(); | 
|---|
| 75 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 76 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 77 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 78 | i = 9; | 
|---|
| 79 | cout << "Seek-tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv | 
|---|
| 80 | << " Pos= " << pos << endl; | 
|---|
| 81 | s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg); | 
|---|
| 82 | pos = s->tellg(); | 
|---|
| 83 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 84 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 85 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 86 | i = 1; | 
|---|
| 87 | cout << "Seek-tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv | 
|---|
| 88 | << " Pos= " << pos << endl; | 
|---|
| 89 |  | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void tread2(istream* s) | 
|---|
| 93 | { | 
|---|
| 94 | int i; | 
|---|
| 95 | int iv; | 
|---|
| 96 | float fv; | 
|---|
| 97 | double dv; | 
|---|
| 98 | long pos; | 
|---|
| 99 | for(i=0; i<7; i++) { | 
|---|
| 100 | pos = s->tellg(); | 
|---|
| 101 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 102 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 103 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 104 | cout << "tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv | 
|---|
| 105 | << " Pos= " << pos << endl; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | cout << " ----- Check Output from here ------- " << endl; | 
|---|
| 109 | s->seekg(0, ios::end); | 
|---|
| 110 | pos = s->tellg(); | 
|---|
| 111 | cout << "Seek-tread2 s->seekg(0, ios::end); Pos = " << pos ; | 
|---|
| 112 | pos -= (sizeof(int)+sizeof(float)+sizeof(double)); | 
|---|
| 113 | cout << " ---> Pos= " << pos << endl; | 
|---|
| 114 | s->seekg(pos, ios::beg); | 
|---|
| 115 | pos = s->tellg(); | 
|---|
| 116 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 117 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 118 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 119 | i = 9; | 
|---|
| 120 | cout << "Seek-tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv | 
|---|
| 121 | << " Pos= " << pos << endl; | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 | s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg); | 
|---|
| 125 | for(i=1; i<3; i++) { | 
|---|
| 126 | pos = s->tellg(); | 
|---|
| 127 | s->read((char *)(&iv), sizeof(int)); | 
|---|
| 128 | s->read((char *)(&fv), sizeof(float)); | 
|---|
| 129 | s->read((char *)(&dv), sizeof(double)); | 
|---|
| 130 |  | 
|---|
| 131 | cout << "Seek-tread2-2 " << i << " IV= " << iv << " FV= " << fv | 
|---|
| 132 | << " DV= " << dv << " Pos= " << pos << endl; | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|