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

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

Programme test de positionnement sur fichier avec les fstream cd C++ - Reza 4/7/2000

File size: 1.9 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);
8void twrite(ostream* s);
9
10int main(int narg, char ** arg)
11{
12if (narg < 2) {
13 cout << "Usage: tios r/w" << endl;
14 exit(0);
15 }
16
17istream* is;
18ostream* os;
19if (arg[1][0] == 'r') {
20// is = new ifstream("tios.dat", ios::in | ios::binary);
21 is = new ifstream("tios.dat", ios::in );
22 tread(is);
23 delete is;
24 }
25else {
26// os = new ofstream("tios.dat", ios::out | ios::binary);
27 os = new ofstream("tios.dat", ios::out );
28 twrite(os);
29 delete(os);
30 }
31
32}
33
34void twrite(ostream* s)
35{
36int i;
37int iv;
38float fv;
39double dv;
40for(i=0; i<10; i++) {
41 iv = 1000+i*50;
42 fv = iv+2.5;
43 dv = iv+3.141596;
44 s->write((char const*)(&iv), sizeof(int));
45 s->write((char const*)(&fv), sizeof(float));
46 s->write((char const*)(&dv), sizeof(double));
47 cout << "twrite " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv << endl;
48 }
49}
50
51void tread(istream* s)
52{
53int i;
54int iv;
55float fv;
56double dv;
57long pos;
58for(i=0; i<10; i++) {
59 pos = s->tellg();
60 s->read((char *)(&iv), sizeof(int));
61 s->read((char *)(&fv), sizeof(float));
62 s->read((char *)(&dv), sizeof(double));
63 cout << "tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
64 << " Pos= " << pos << endl;
65 }
66
67s->seekg(-(sizeof(int)+sizeof(float)+sizeof(double)), ios::end);
68pos = s->tellg();
69s->read((char *)(&iv), sizeof(int));
70s->read((char *)(&fv), sizeof(float));
71s->read((char *)(&dv), sizeof(double));
72i = 9;
73cout << "Seek-tread " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
74 << " Pos= " << pos << endl;
75s->seekg((sizeof(int)+sizeof(float)+sizeof(double)), ios::beg);
76pos = s->tellg();
77s->read((char *)(&iv), sizeof(int));
78s->read((char *)(&fv), sizeof(float));
79s->read((char *)(&dv), sizeof(double));
80i = 1;
81cout << "Seek-tread2 " << i << " IV= " << iv << " FV= " << fv << " DV= " << dv
82 << " Pos= " << pos << endl;
83
84}
85
86
Note: See TracBrowser for help on using the repository browser.