-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.cpp
More file actions
40 lines (26 loc) · 861 Bytes
/
fileio.cpp
File metadata and controls
40 lines (26 loc) · 861 Bytes
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
#include <stdio.h>
#include "fileio.h"
#include "structures.h"
void read_block(char *buffer, int block_number, char *disk) {
FILE *harddisk = fopen(disk, "rb+");
if (!harddisk) {
printf("fileio: Cannot open harddisk\n");
return;
}
int block_offset = block_number * BLOCKSIZE;
fseek(harddisk, block_offset, SEEK_SET);
fread(buffer, BLOCKSIZE, 1, harddisk);
fclose(harddisk);
}
void write_block(char *buffer, int block_number, char *disk, int last_block) {
FILE *harddisk = fopen(disk, "rb+");
if (!harddisk) {
printf("fileio: Cannot open harddisk\n");
return;
}
int block_offset = block_number * BLOCKSIZE;
fseek(harddisk, block_offset, SEEK_SET);
int size = last_block != 0 ? last_block : BLOCKSIZE;
fwrite(buffer, size, 1, harddisk);
fclose(harddisk);
}