-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (95 loc) · 2.73 KB
/
main.cpp
File metadata and controls
142 lines (95 loc) · 2.73 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
//Setting the i-th bit to either 1 or 0
//Bit i will be set if val is 1, and cleared if x is 0
int setBit(int i, char val, int bits);
int getBit(int i, int val);
int setBit(int i, char val, int bits){
if(val == 1){
int a =1;
a = a << i;
bits = bits | a;
}
if(val == 0){
int a = 1;
a = a << i;
a = ~a;
bits = bits & a;
}
return bits;
}
int getBit(int i, int val){
val = val & ( 1 << i );
if(val == 0){
return 0;
}
else if(val > 0){
return 1;
}
}
int main(){
int size = 5;
char c[size];
c[0] = 7;
c[1] = 1;
c[2] = 5;
c[3] = 2;
c[4] = 3;
c[5] = 7;
char d[2] = {};
int j = 2;
int num = 0;
int ch = 0;
while(num < 5+1){
for(int i = 7; i >= 0; j--, i--){
d[ch] = setBit(i, getBit(j, c[num]), d[ch]);
if(j == 0){
num++;
j = 3;
}
}
ch++;
}
FILE* ptrFile = fopen ("text" , "wb");
fwrite(d, 1, sizeof(d+1), ptrFile ); // записать в файл содержимое буфера
fclose (ptrFile);
ptrFile = fopen( "text" , "rb" );
if (ptrFile == NULL){
fputs("Ошибка файла!!!!!\n", stderr);
exit(1);
}
// определяем размер файла
fseek(ptrFile , 0 , SEEK_END); // устанавливаем позицию в конец файла
long lSize = ftell(ptrFile); // получаем размер в байтах
rewind (ptrFile); // устанавливаем указатель в конец файла
char buffer[lSize];
if (buffer == NULL){
fputs("Ошибка памяти!!!\n", stderr);
exit(2);
}
size_t result = fread(buffer, 1, lSize, ptrFile);
if (result != lSize){
fputs("Ошибка чтения!!!!\n", stderr);
exit (3);
}
fclose (ptrFile);
num = 0;
char newc[100] = {0};
ch = 0;
while(num < 2+1){//количество чисел в buffer
for(int i = 7; i >= 0; j--, i--){//идем по битам в каждом числе
newc[ch] = setBit(j, getBit(i, buffer[num]), newc[ch]); // берем биты и записываем в последние три бита каждого числа
if(j == 0){;
ch++;
j = 3;
}
}
num++;
}
for(int i = 0; i < 6; i++){
cout << (int)newc[i] << endl;
}
return 0;
}