source: Sophya/trunk/Eval/COS/tios.cc@ 2428

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

amelioration du programme test/check seekg() sur iostream - Reza 25/4/2002

File size: 3.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4#include <iostream.h>
5#include <fstream.h>
6
7void tread(istream* s);
8void tread2(istream* s);
9void twrite(ostream* s);
10
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
25int main(int narg, char ** arg)
26{
27if (narg < 2) {
28 cout << "Usage: tios w/r/r2 \n"
29 << " w : Write data file tios.cc \n"
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;
32 exit(0);
33 }
34
35istream* is;
36ostream* os;
37if (arg[1][0] == 'r') {
38// is = new ifstream("tios.dat", ios::in | ios::binary);
39 is = new ifstream("tios.dat", ios::in );
40 if ( arg[1][1] == '2') tread2(is);
41 else tread(is);
42 delete is;
43 }
44else {
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
53void twrite(ostream* s)
54{
55int i;
56int iv;
57float fv;
58double dv;
59for(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
70void tread(istream* s)
71{
72int i;
73int iv;
74float fv;
75double dv;
76long pos;
77for(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
86cout << " ----- Check Output from here ------- " << endl;
87for(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}
96s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg);
97pos = s->tellg();
98s->read((char *)(&iv), sizeof(int));
99s->read((char *)(&fv), sizeof(float));
100s->read((char *)(&dv), sizeof(double));
101cout << "Seek-tread-begin " << " IV= " << iv << " FV= " << fv << " DV= " << dv
102 << " Pos= " << pos << endl;
103
104}
105
106void tread2(istream* s)
107{
108int i;
109int iv;
110float fv;
111double dv;
112long pos;
113for(i=0; i<10; i++) {
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 }
121
122cout << " ----- Check Output from here ------- " << endl;
123for(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
134s->seekg(0, ios::end);
135pos = s->tellg();
136cout << "Seek-tread2 s->seekg(0, ios::end); Pos = " << pos ;
137pos -= (sizeof(int)+sizeof(float)+sizeof(double));
138cout << " ---> Pos= " << pos << endl;
139s->seekg(pos, ios::beg);
140pos = s->tellg();
141s->read((char *)(&iv), sizeof(int));
142s->read((char *)(&fv), sizeof(float));
143s->read((char *)(&dv), sizeof(double));
144i = 9;
145cout << "Seek-tread2-begin " << " IV= " << iv << " FV= " << fv << " DV= " << dv
146 << " Pos= " << pos << endl;
147
148
149}
Note: See TracBrowser for help on using the repository browser.