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