-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage.c
48 lines (42 loc) · 1.06 KB
/
garbage.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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int save_shorts(const unsigned short* array, size_t num_shorts)
{
int ok = 0;
FILE* out = fopen("numbers.bin", "wb");
if (out != NULL)
{
ok = fwrite(array, num_shorts * sizeof *array, 1, out) == 1;
fclose(out);
}
return ok;
}
void readFile(const char * fname, unsigned short * array, unsigned length)
{
std::ifstream fin(fname, std::ios::binary);
fin.seekg(0, std::ios_base::end);
unsigned file_len = fin.tellg();
if (file_len != length * sizeof(unsigned short))
{
std::cout << "Error: file length: " << file_len
<< " expected: " << length * sizeof(unsigned short) << std::endl;
return;
}
fin.seekg(0, std::ios_base::beg);
fin.read( (char *) array, file_len);
fin.close();
}
int main()
{
cout << "Hello world!" << endl;
int num = 2;
unsigned short sArr[2];
sArr[0]=123;
sArr[1]=456;
save_shorts(sArr,2);
unsigned short buf[2] = {0};
readFile("numbers.bin", buf, 2);
return 0;
}