-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_run.cpp
56 lines (41 loc) · 1.19 KB
/
sample_run.cpp
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
//Sample file for students to get their code running
#include<iostream>
#include "file_manager.h"
#include "errors.h"
#include<cstring>
using namespace std;
int main() {
FileManager fm;
// Create a brand new file
FileHandler fh = fm.CreateFile("temp.txt");
cout << "File created " << endl;
// Create a new page
PageHandler ph = fh.NewPage ();
char *data = ph.GetData ();
// Store an integer at the very first location
int num = 5;
memcpy (&data[0], &num, sizeof(int));
// Store an integer at the second location
num = 1000;
memcpy (&data[4], &num, sizeof(int));
// Flush the page
fh.FlushPages ();
cout << "Data written and flushed" << endl;
// Close the file
fm.CloseFile(fh);
// Reopen the same file, but for reading this time
fh = fm.OpenFile ("temp.txt");
cout << "File opened" << endl;
// Get the very first page and its data
ph = fh.FirstPage ();
data = ph.GetData ();
// Output the first integer
memcpy (&num, &data[0], sizeof(int));
cout << "First number: " << num << endl;
// Output the second integer
memcpy (&num, &data[4], sizeof(int));
cout << "Second number: " << num << endl;;
// Close the file and destory it
fm.CloseFile (fh);
fm.DestroyFile ("temp.txt");
}