| [1966] | 1 | #include <stdio.h> | 
|---|
|  | 2 | #include <stdlib.h> | 
|---|
|  | 3 | #include <math.h> | 
|---|
| [2322] | 4 | #include <iostream> | 
|---|
|  | 5 | #include <fstream> | 
|---|
| [1966] | 6 |  | 
|---|
| [2322] | 7 | using namespace std; | 
|---|
|  | 8 |  | 
|---|
| [1966] | 9 | int tstseqwrite(); | 
|---|
|  | 10 | int tstseqread(); | 
|---|
|  | 11 | int tstseekbegread(); | 
|---|
|  | 12 | int tstseekendread(); | 
|---|
|  | 13 |  | 
|---|
|  | 14 | // ----------------------------------------------------- | 
|---|
|  | 15 | // This test program illustrates the problem with g++ | 
|---|
|  | 16 | //  (pb with g++ 2.95 3.0) | 
|---|
|  | 17 | // iostream library with seekg(..., ios::end) | 
|---|
|  | 18 | //         R. Ansari (LAL/IN2P3-CNRS) - April 2002 | 
|---|
|  | 19 | // | 
|---|
|  | 20 | // # compile the program | 
|---|
|  | 21 | // csh> g++ -o pbseekios pbseekios.cc | 
|---|
|  | 22 | // # Run the program, check the output and the return code | 
|---|
|  | 23 | // # Read with seekg(... ios::end) ---> wrong result (tstseekendread()) | 
|---|
|  | 24 | // # while seekg(... ios::beg) works OK (tstseekbegread()) | 
|---|
|  | 25 | // csh> ./pbseekios | 
|---|
| [2428] | 26 | // September 2003 | 
|---|
|  | 27 | // The problem with seekg(... ios::end) seems to be solved | 
|---|
|  | 28 | // with gcc 3.1, | 
|---|
|  | 29 | // However, on MacOSX 10.2, with gcc 3.1, reading the last byte of | 
|---|
|  | 30 | // a file causes an error and stream can not be positionned anymore | 
|---|
| [1966] | 31 | // ---------------------------------------------------- | 
|---|
|  | 32 |  | 
|---|
|  | 33 | int main(int narg, char ** arg) | 
|---|
|  | 34 | { | 
|---|
|  | 35 |  | 
|---|
|  | 36 | int rc; | 
|---|
|  | 37 | rc = tstseqwrite(); | 
|---|
|  | 38 | rc += tstseqread(); | 
|---|
|  | 39 | rc += tstseekbegread(); | 
|---|
|  | 40 | rc += tstseekendread(); | 
|---|
|  | 41 | if (rc == 0) | 
|---|
|  | 42 | cout << "--- End of pbseekios - OK (Rc=0) " << endl; | 
|---|
|  | 43 | else | 
|---|
|  | 44 | cout << "--- End of pbseekios - ERRORS (Rc= " << rc << ")" << endl; | 
|---|
|  | 45 | return (rc); | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
|  | 48 | // ------ Test function to write sequentially to a ofstream ------ | 
|---|
|  | 49 | int tstseqwrite() | 
|---|
|  | 50 | { | 
|---|
|  | 51 | int i; | 
|---|
|  | 52 | int iv; | 
|---|
|  | 53 | float fv; | 
|---|
|  | 54 | char * flnm = "pbseekios.dat"; | 
|---|
| [2428] | 55 | ofstream os(flnm, ios::out | ios::binary); | 
|---|
| [1966] | 56 | cout << ">>>> tstseqwrite() - Sequential writing to file " << flnm << endl; | 
|---|
| [2428] | 57 | //  ofstream os(flnm, ios::out ); | 
|---|
| [1966] | 58 | for(i=0; i<7; i++) { | 
|---|
|  | 59 | iv = 1000+i*10; | 
|---|
|  | 60 | fv = iv+0.5; | 
|---|
|  | 61 | os.write((char const*)(&iv), sizeof(int)); | 
|---|
|  | 62 | os.write((char const*)(&fv), sizeof(float)); | 
|---|
|  | 63 | } | 
|---|
|  | 64 | return(0); | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // ------ Test function to read sequentially from a ifstream ------ | 
|---|
|  | 68 | int tstseqread() | 
|---|
|  | 69 | { | 
|---|
|  | 70 | int i; | 
|---|
|  | 71 | int iv,iv0;; | 
|---|
|  | 72 | float fv, fv0; | 
|---|
|  | 73 | long pos; | 
|---|
|  | 74 | char * flnm = "pbseekios.dat"; | 
|---|
|  | 75 | // is = new ifstream(flnm, ios::in | ios::binary); | 
|---|
|  | 76 | ifstream is(flnm, ios::in ); | 
|---|
|  | 77 | cout << ">>>> tstseqread() - Sequential reading from file " << flnm << endl; | 
|---|
|  | 78 | int rc = 0; | 
|---|
|  | 79 | for(i=0; i<7; i++) { | 
|---|
|  | 80 | iv0 = 1000+i*10; | 
|---|
|  | 81 | fv0 = iv0 + 0.5; | 
|---|
|  | 82 | iv = 0;  fv = 0.; | 
|---|
|  | 83 | pos = is.tellg(); | 
|---|
|  | 84 | is.read((char *)(&iv), sizeof(int)); | 
|---|
|  | 85 | is.read((char *)(&fv), sizeof(float)); | 
|---|
|  | 86 | cout << "Read@" << i << " : Pos= " << pos << " IV=" << iv | 
|---|
|  | 87 | << " FV= " << fv << " Expect:(" | 
|---|
| [2428] | 88 | << iv0 << "," << fv0 << ")" | 
|---|
|  | 89 | << " ->NewPos="<< is.tellg() << endl; | 
|---|
| [1966] | 90 | if (iv != iv0) rc++; | 
|---|
|  | 91 | } | 
|---|
|  | 92 | if (rc == 0) | 
|---|
|  | 93 | cout << " tstseqread() OK , Rc= 0 " << endl; | 
|---|
|  | 94 | else | 
|---|
|  | 95 | cout << " tstseqread() ERROR , Rc=  " << rc << endl; | 
|---|
|  | 96 | return rc; | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | // ------ Random access read from a ifstream seek(ios::beg) ------ | 
|---|
|  | 100 | int tstseekbegread() | 
|---|
|  | 101 | { | 
|---|
|  | 102 | int i; | 
|---|
|  | 103 | int num[7] = {2,4,1,6,5,3,0}; | 
|---|
|  | 104 | int iv,iv0; | 
|---|
|  | 105 | float fv,fv0; | 
|---|
|  | 106 | long pos; | 
|---|
|  | 107 | char * flnm = "pbseekios.dat"; | 
|---|
|  | 108 | // is = new ifstream(flnm, ios::in | ios::binary); | 
|---|
|  | 109 | ifstream is(flnm, ios::in ); | 
|---|
|  | 110 | cout << ">>>> tstseekbegread() - Random access reading from file " | 
|---|
|  | 111 | << flnm << " (seekg(... ios::beg) " << endl; | 
|---|
|  | 112 | int rc = 0; | 
|---|
|  | 113 | for(i=0; i<7; i++) { | 
|---|
|  | 114 | iv0 = 1000+num[i]*10; | 
|---|
|  | 115 | fv0 = iv0 + 0.5; | 
|---|
|  | 116 | iv = 0;  fv = 0.; | 
|---|
|  | 117 | is.seekg(num[i]*(sizeof(int)+sizeof(float)), ios::beg); | 
|---|
|  | 118 | pos = is.tellg(); | 
|---|
|  | 119 | is.read((char *)(&iv), sizeof(int)); | 
|---|
|  | 120 | is.read((char *)(&fv), sizeof(float)); | 
|---|
|  | 121 | cout << "Read@" << num[i] << " : Pos= " << pos << " IV=" << iv | 
|---|
|  | 122 | << " FV= " << fv << " Expect:(" | 
|---|
| [2428] | 123 | << iv0 << "," << fv0 << ")" | 
|---|
|  | 124 | << " ->NewPos="<< is.tellg() << endl; | 
|---|
| [1966] | 125 | if (iv != iv0) rc++; | 
|---|
|  | 126 | } | 
|---|
|  | 127 | if (rc == 0) | 
|---|
|  | 128 | cout << " tstseekbegread() OK , Rc= 0 " << endl; | 
|---|
|  | 129 | else | 
|---|
|  | 130 | cout << " tstseekbegread() ERROR , Rc=  " << rc << endl; | 
|---|
|  | 131 | return rc; | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | // ------ Random access read from a ifstream seek(ios::end) ------ | 
|---|
|  | 135 | int tstseekendread() | 
|---|
|  | 136 | { | 
|---|
|  | 137 | int i; | 
|---|
|  | 138 | int num[7] = {2,4,1,6,5,3,0}; | 
|---|
|  | 139 | int iv,iv0; | 
|---|
|  | 140 | float fv,fv0; | 
|---|
|  | 141 | long pos; | 
|---|
|  | 142 | char * flnm = "pbseekios.dat"; | 
|---|
|  | 143 | // is = new ifstream(flnm, ios::in | ios::binary); | 
|---|
|  | 144 | ifstream is(flnm, ios::in ); | 
|---|
|  | 145 | cout << ">>>> tstseekendread() - Random access reading from file " | 
|---|
|  | 146 | << flnm << " (seekg(... ios::end) " << endl; | 
|---|
|  | 147 | int rc = 0; | 
|---|
|  | 148 | for(i=0; i<7; i++) { | 
|---|
|  | 149 | iv0 = 1000+num[i]*10; | 
|---|
|  | 150 | fv0 = iv0 + 0.5; | 
|---|
|  | 151 | iv = 0;  fv = 0.; | 
|---|
|  | 152 | is.seekg((num[i]-7)*(sizeof(int)+sizeof(float)), ios::end); | 
|---|
|  | 153 | pos = is.tellg(); | 
|---|
|  | 154 | is.read((char *)(&iv), sizeof(int)); | 
|---|
|  | 155 | is.read((char *)(&fv), sizeof(float)); | 
|---|
|  | 156 | cout << "Read@" << num[i] << " : Pos= " << pos << " IV=" << iv | 
|---|
|  | 157 | << " FV= " << fv << " Expect:(" | 
|---|
| [2428] | 158 | << iv0 << "," << fv0 << ")" | 
|---|
|  | 159 | << " ->NewPos="<< is.tellg() << endl; | 
|---|
| [1966] | 160 | if (iv != iv0) rc++; | 
|---|
|  | 161 | } | 
|---|
|  | 162 | if (rc == 0) | 
|---|
|  | 163 | cout << " tstseekendread() OK , Rc= 0 " << endl; | 
|---|
|  | 164 | else | 
|---|
|  | 165 | cout << " tstseekendread() ERROR , Rc=  " << rc << endl; | 
|---|
|  | 166 | return rc; | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|