DZ13_2

 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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
FILE *file;
file = fopen("test.txt", "rb");
char str[6];
fseek(file, 0, SEEK_END);
int len = ftell(file);
fseek(file, 0, SEEK_SET);
int isk = 893;
//int cur = 0;
printf("len = %i\n", len);
int countOver = 0;
int center = len/2-6;
int cur = center;
fseek(file, cur, SEEK_SET);
while (true) {
if (cur == 0 && countOver++) break;
if (!fgets(str, 6, file)) break;
if (cur > len && countOver++) break;
int a = atoi(str);
puts(str);
if (a == isk) {
printf("!!!!!!!!!!!!");
break;
} else if (a > isk) {
if (cur > center) {
cur = cur - (len-cur)-6;
} else {
cur = cur/2-6;
if (cur < 0) cur = 0;
}
} else {
if (cur > center) {
cur += (len-cur)/2-6;
if (cur > len) cur = len-6;
}
else
cur += cur/2-6;
}
fseek(file, cur, SEEK_SET);
printf("cur = %i\n", cur);
}
getch();
}