1 | #include <stdio.h>
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include <math.h>
|
---|
4 | #include <iostream>
|
---|
5 | #include <fstream>
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
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
|
---|
26 | // ----------------------------------------------------
|
---|
27 |
|
---|
28 | int main(int narg, char ** arg)
|
---|
29 | {
|
---|
30 |
|
---|
31 | int rc;
|
---|
32 | rc = tstseqwrite();
|
---|
33 | rc += tstseqread();
|
---|
34 | rc += tstseekbegread();
|
---|
35 | rc += tstseekendread();
|
---|
36 | if (rc == 0)
|
---|
37 | cout << "--- End of pbseekios - OK (Rc=0) " << endl;
|
---|
38 | else
|
---|
39 | cout << "--- End of pbseekios - ERRORS (Rc= " << rc << ")" << endl;
|
---|
40 | return (rc);
|
---|
41 | }
|
---|
42 |
|
---|
43 | // ------ Test function to write sequentially to a ofstream ------
|
---|
44 | int tstseqwrite()
|
---|
45 | {
|
---|
46 | int i;
|
---|
47 | int iv;
|
---|
48 | float fv;
|
---|
49 | char * flnm = "pbseekios.dat";
|
---|
50 | // ofstream os(flnm, ios::out | ios::binary);
|
---|
51 | cout << ">>>> tstseqwrite() - Sequential writing to file " << flnm << endl;
|
---|
52 | ofstream os(flnm, ios::out );
|
---|
53 | for(i=0; i<7; i++) {
|
---|
54 | iv = 1000+i*10;
|
---|
55 | fv = iv+0.5;
|
---|
56 | os.write((char const*)(&iv), sizeof(int));
|
---|
57 | os.write((char const*)(&fv), sizeof(float));
|
---|
58 | }
|
---|
59 | return(0);
|
---|
60 | }
|
---|
61 |
|
---|
62 | // ------ Test function to read sequentially from a ifstream ------
|
---|
63 | int tstseqread()
|
---|
64 | {
|
---|
65 | int i;
|
---|
66 | int iv,iv0;;
|
---|
67 | float fv, fv0;
|
---|
68 | long pos;
|
---|
69 | char * flnm = "pbseekios.dat";
|
---|
70 | // is = new ifstream(flnm, ios::in | ios::binary);
|
---|
71 | ifstream is(flnm, ios::in );
|
---|
72 | cout << ">>>> tstseqread() - Sequential reading from file " << flnm << endl;
|
---|
73 | int rc = 0;
|
---|
74 | for(i=0; i<7; i++) {
|
---|
75 | iv0 = 1000+i*10;
|
---|
76 | fv0 = iv0 + 0.5;
|
---|
77 | iv = 0; fv = 0.;
|
---|
78 | pos = is.tellg();
|
---|
79 | is.read((char *)(&iv), sizeof(int));
|
---|
80 | is.read((char *)(&fv), sizeof(float));
|
---|
81 | cout << "Read@" << i << " : Pos= " << pos << " IV=" << iv
|
---|
82 | << " FV= " << fv << " Expect:("
|
---|
83 | << iv0 << "," << fv0 << ")" << endl;
|
---|
84 | if (iv != iv0) rc++;
|
---|
85 | }
|
---|
86 | if (rc == 0)
|
---|
87 | cout << " tstseqread() OK , Rc= 0 " << endl;
|
---|
88 | else
|
---|
89 | cout << " tstseqread() ERROR , Rc= " << rc << endl;
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // ------ Random access read from a ifstream seek(ios::beg) ------
|
---|
94 | int tstseekbegread()
|
---|
95 | {
|
---|
96 | int i;
|
---|
97 | int num[7] = {2,4,1,6,5,3,0};
|
---|
98 | int iv,iv0;
|
---|
99 | float fv,fv0;
|
---|
100 | long pos;
|
---|
101 | char * flnm = "pbseekios.dat";
|
---|
102 | // is = new ifstream(flnm, ios::in | ios::binary);
|
---|
103 | ifstream is(flnm, ios::in );
|
---|
104 | cout << ">>>> tstseekbegread() - Random access reading from file "
|
---|
105 | << flnm << " (seekg(... ios::beg) " << endl;
|
---|
106 | int rc = 0;
|
---|
107 | for(i=0; i<7; i++) {
|
---|
108 | iv0 = 1000+num[i]*10;
|
---|
109 | fv0 = iv0 + 0.5;
|
---|
110 | iv = 0; fv = 0.;
|
---|
111 | is.seekg(num[i]*(sizeof(int)+sizeof(float)), ios::beg);
|
---|
112 | pos = is.tellg();
|
---|
113 | is.read((char *)(&iv), sizeof(int));
|
---|
114 | is.read((char *)(&fv), sizeof(float));
|
---|
115 | cout << "Read@" << num[i] << " : Pos= " << pos << " IV=" << iv
|
---|
116 | << " FV= " << fv << " Expect:("
|
---|
117 | << iv0 << "," << fv0 << ")" << endl;
|
---|
118 | if (iv != iv0) rc++;
|
---|
119 | }
|
---|
120 | if (rc == 0)
|
---|
121 | cout << " tstseekbegread() OK , Rc= 0 " << endl;
|
---|
122 | else
|
---|
123 | cout << " tstseekbegread() ERROR , Rc= " << rc << endl;
|
---|
124 | return rc;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // ------ Random access read from a ifstream seek(ios::end) ------
|
---|
128 | int tstseekendread()
|
---|
129 | {
|
---|
130 | int i;
|
---|
131 | int num[7] = {2,4,1,6,5,3,0};
|
---|
132 | int iv,iv0;
|
---|
133 | float fv,fv0;
|
---|
134 | long pos;
|
---|
135 | char * flnm = "pbseekios.dat";
|
---|
136 | // is = new ifstream(flnm, ios::in | ios::binary);
|
---|
137 | ifstream is(flnm, ios::in );
|
---|
138 | cout << ">>>> tstseekendread() - Random access reading from file "
|
---|
139 | << flnm << " (seekg(... ios::end) " << endl;
|
---|
140 | int rc = 0;
|
---|
141 | for(i=0; i<7; i++) {
|
---|
142 | iv0 = 1000+num[i]*10;
|
---|
143 | fv0 = iv0 + 0.5;
|
---|
144 | iv = 0; fv = 0.;
|
---|
145 | is.seekg((num[i]-7)*(sizeof(int)+sizeof(float)), ios::end);
|
---|
146 | pos = is.tellg();
|
---|
147 | is.read((char *)(&iv), sizeof(int));
|
---|
148 | is.read((char *)(&fv), sizeof(float));
|
---|
149 | cout << "Read@" << num[i] << " : Pos= " << pos << " IV=" << iv
|
---|
150 | << " FV= " << fv << " Expect:("
|
---|
151 | << iv0 << "," << fv0 << ")" << endl;
|
---|
152 | if (iv != iv0) rc++;
|
---|
153 | }
|
---|
154 | if (rc == 0)
|
---|
155 | cout << " tstseekendread() OK , Rc= 0 " << endl;
|
---|
156 | else
|
---|
157 | cout << " tstseekendread() ERROR , Rc= " << rc << endl;
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|