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

Last change on this file since 1060 was 1060, checked in by ansari, 25 years ago

ajout contournement pb ifstream::seekg() pour Linux ds tios.cc - Reza 11/7/2000

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