-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLab3.cpp
More file actions
136 lines (123 loc) · 2.86 KB
/
Lab3.cpp
File metadata and controls
136 lines (123 loc) · 2.86 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Lab3.cpp
*
* Created on: Sep 26, 2013
* Author: leon
*/
#include <cstdlib>
#include <string>
#include <cctype>
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cstring>
#define IDSIZE 10
using namespace std;
struct Record {
char id[IDSIZE];
int score;
};
////////////////////////////////////////////////////////////////////////////////
bool is_valid_ID(const string& id);
bool is_valid_score(int score);
bool get_valid_word(const string& prompt, string& word,
bool (*is_valid)(const string&) = 0);
bool get_valid_int(const string& prompt, int& n, bool (*is_valid)(int) = 0);
///////////////////////////////////////////////////////////////////////////////////
bool get_command(const string& prompt, int& n) {
string line;
while (1) {
cout << prompt;
if (!getline(cin, line))
break;
istringstream iss(line);
if (iss >> n) {
if (n == 0) {
return false;
}
return true;
}
cout << "Invalid command" << endl;
}
cin.clear();
return false;
}
void display_n(fstream& file, const int n) {
string id;
ios::pos_type pos = sizeof(Record) * (n - 1);
file.seekg(pos, ios_base::beg);
Record r;
file.read(r.id, sizeof(Record));
if (file) {
string id(r.id);
cout << "id: " << id << " score: " << r.score << endl;
return;
}
cout << "seek past the EOF." << endl;
file.clear();
}
void display_all_fromn(fstream& file, const int n) {
string id;
ios::pos_type pos = sizeof(Record) * (n - 1);
file.seekg(pos, ios_base::beg);
Record r;
file.read((char *)&r, sizeof(Record));
if (!file) {
cout << "seek past the EOF." << endl;
file.clear();
return;
} else {
string id(r.id);
cout << "id: " << id << " score: " << r.score << endl;
}
while (1) {
file.read(r.id, sizeof(Record));
if (!file) {
file.clear();
return;
}else {
string id(r.id);
cout << "id: " << id << " score: " << r.score << endl;
}
}
}
/*
*
*/
int lab3_main(int argc, char** argv) {
Record sr;
int n;
cout << argv[1] << endl;
cout << "size of int: " << sizeof(int) << endl;
fstream file(argv[1],
ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary);
if (!file) {
cerr << "unable to open students.dat file." << endl;
return 1;
}
while (get_command("\n1.Input records\n2.Display records\n", n)) {
if (n == 1) {
string srstring;
while (get_valid_word("please inter ID. \n ", srstring, is_valid_ID)
&& get_valid_int("please inter score \n ", sr.score,
is_valid_score)) {
strcpy(sr.id, srstring.c_str());
//this is a error, you have to seek to the postion you will write.
file.write(sr.id, sizeof(Record));
}
} else if (n == 2) {
while (get_command("please input a number: ", n)) {
if (n > 0) {
display_n(file, n);
} else {
display_all_fromn(file, -n);
}
}
} else {
cout << "Invalid command" << endl;
}
}
return 0;
}