-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrandom.cpp
More file actions
109 lines (98 loc) · 3.01 KB
/
random.cpp
File metadata and controls
109 lines (98 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// rnadom.cpp 随机访问二进制文件
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
const int LIM = 20;
struct planet
{
char name[LIM]; // name of planet
double population; // its population
double g; // its acceleration of gravity
};
const char * file = "planets.dat"; // assumed to exist
inline void eatline() {while (std::cin.get() != '\n') continue;}
int main()
{
using namespace std;
planet pl;
cout << fixed;
// show initial contents
fstream finout; // read and write streams
finout.open(file, ios_base::in | ios_base::out | ios_base::binary);
// NOTES : Some Unix Sytems require omitting | ios_base::binary
int ct = 0;
if (finout.is_open())
{
finout.seekg(0); // go to beginning
cout << " Here are the current contents of the "
<< file << " file : \n";
while (finout.read((char *) &pl, sizeof pl));
cout << ct++ << " : " << setw(LIM) << pl.name << " : "
<< setprecision(0) << setw(LIM) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
if (finout.eof())
finout.clear(); // clear of flag
else
{
cerr << "Error in Reading " << file << ".\n";
exit(EXIT_FAILURE);
}
}
else
{
cerr << file << "could not be opened ----- bye .\n";
exit(EXIT_FAILURE);
}
// change a record
cout << "Enter the record number you wish to change : ";
long rec;
cin >> rec;
eatline();
if (rec < 0 || rec >= ct)
{
cerr << "Invalid record number ---- bye \n";
exit(EXIT_FAILURE);
}
streampos place = rec * sizeof pl; // 转换为 streampos type
finout.seekg(place); // random access
if (finout.fail())
{
cerr << " Error on attemped seek \n";
exit(EXIT_FAILURE);
}
finout.read((char *) &pl, sizeof pl);
cout << "Your selection:\n";
cout << rec << ":" << setw(LIM) << pl.name << ":"
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
if (finout.eof())
finout.clear(); // clear of flag
cout << "Enter planet name : ";
cin.get(pl.name,LIM);
eatline();
cout << "Enter planetary population : ";
cin >> pl.population;
cout << "Enter planet's acceleration of gravity : ";
cin >> pl.g;
finout.seekg(place); // go back
finout.write((char *) &pl,sizeof pl) << flush;
if(finout.fail())
{
cerr << "Error on attempted write \n";
exit(EXIT_FAILURE);
}
// show revised file
ct = 0;
finout.seekg(0);
cout << "Here are the new contents of the " << file << " file; \n";
while (finout.read((char *) &pl, sizeof pl))
{
cout << ct++ << " : " << setw(LIM) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
finout.close();
cout << "Done. \n";
return 0;
}