source: Sophya/trunk/Eval/COS/pbseekios.cc@ 1966

Last change on this file since 1966 was 1966, checked in by ansari, 23 years ago

Ajout programmes test de pb de compilation g++ 2.95/3.01 - Reza 25/4/2002

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