-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.h
81 lines (64 loc) · 2.17 KB
/
File.h
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
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cerrno>
#include <cstring>
#include <string>
#include <vector>
class File
{
public:
//==========================================================================
// Constructor
//==========================================================================
File(const char filename[1024], int page_size = 4)
:page_size_(page_size)
{
strcpy(filename_, filename);
fd_ = open(filename, O_RDWR | O_CREAT, 0644);
if (fd_ < 0)
{
std::cout << "open fail\n";
std::cout << "errno: " << errno << std::endl;
std::cout << "strerror: " << strerror(errno) << std::endl;
}
}
//==========================================================================
// Destructor
//==========================================================================
~File() { close_file(); }
//==========================================================================
// GET Methods
//==========================================================================
std::string filename() const { return filename_; }
int fd() const { return fd_; }
int get_page_size() const { return page_size_; }
//==========================================================================
// RDWR Methods
//==========================================================================
void read_file(int page, char buff[])
{
lseek(fd_, page * page_size_, SEEK_CUR);
read(fd_, buff, page_size_);
lseek(fd_, 0, SEEK_SET);
}
void write_file(int page, char buff[])
{
lseek(fd_, page * page_size_, SEEK_CUR);
write(fd_, buff, page_size_);
lseek(fd_, 0, SEEK_SET);
}
void close_file() { close(fd_); }
private:
//==========================================================================
// Member Variables
//==========================================================================
char filename_[1024];
int fd_;
int page_size_;
};
#endif