-
Notifications
You must be signed in to change notification settings - Fork 3
/
fileio.c
71 lines (53 loc) · 1.21 KB
/
fileio.c
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
#include <stdio.h>
void fwrite8(FILE *file, unsigned data)
{
unsigned char bytes[1];
bytes[0] = data;
fwrite(bytes, 1, 1, file);
}
void fwritele16(FILE *file, unsigned data)
{
unsigned char bytes[2];
bytes[0] = data;
bytes[1] = data >> 8;
fwrite(bytes, 2, 1, file);
}
void fwritele32(FILE *file, unsigned data)
{
unsigned char bytes[4];
bytes[0] = data;
bytes[1] = data >> 8;
bytes[2] = data >> 16;
bytes[3] = data >> 24;
fwrite(bytes, 4, 1, file);
}
unsigned fread8(FILE *file)
{
unsigned char bytes[1];
fread(bytes, 1, 1, file);
return bytes[0];
}
unsigned freadle16(FILE *file)
{
unsigned char bytes[2];
fread(bytes, 2, 1, file);
return (bytes[0]) | (bytes[1] << 8);
}
unsigned freadle32(FILE *file)
{
unsigned char bytes[4];
fread(bytes, 4, 1, file);
return (bytes[0]) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
}
unsigned freadhe16(FILE *file)
{
unsigned char bytes[2];
fread(bytes, 2, 1, file);
return (bytes[1]) | (bytes[0] << 8);
}
unsigned freadhe32(FILE *file)
{
unsigned char bytes[4];
fread(bytes, 4, 1, file);
return (bytes[3]) | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24);
}