-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRead4.cpp
58 lines (46 loc) · 997 Bytes
/
Read4.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
57
58
#include "headers.h"
FILE *fpRead4 = nullptr;
int read4(char *target)
{
int n = fread(target, 1, 4, fpRead4);
return n;
}
int readDataWithRead4(char *buf, int n)
{
memset(buf, 0, n);
int N = n / 4;
int location = 0;
bool bDone = false;
for (int i = 0; i < N; i++)
{
int read = read4(&buf[location]);
location += read;
if (read == 0)
{
bDone = true;
break;
}
}
int R = n % 4;
if (!bDone && R > 0)
{
char c[4];
int read = read4(c);
int write = std::min(R, read);
strncpy(&buf[location], c, write);
location += write;
}
return location;
}
void Read4Main()
{
char sFileName [] = "testRead4.bin";
FILE *fp = fopen(sFileName, "wb");
char sData[] = "abcde";
fwrite(&sData, 1, 5, fp);
fclose(fp);
fpRead4 = fopen(sFileName, "rb");
char sBuffer[6];
readDataWithRead4(sBuffer, 5);
fclose(fpRead4);
}