forked from Oricadu/labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththirdLab.c
More file actions
84 lines (59 loc) · 1.54 KB
/
thirdLab.c
File metadata and controls
84 lines (59 loc) · 1.54 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char const *argv[])
{
char name[] = "file.txt";
int strLength = 0, num, i = 0, position = 0, k = 0;
int file1;
file1 = open(name, O_RDONLY);
int len = lseek(file1, 0, SEEK_END);
void *src;
char *findPtr;
if((src = mmap(0, len, PROT_READ, MAP_SHARED, file1, 0)) == MAP_FAILED){
perror("mmap error");
}
int *lengths = NULL;
int *positions = NULL;
lengths = (int*)malloc(1 * sizeof(int));
findPtr = src;
while(findPtr[k] != 0){
if (findPtr[k] == '\n')
{
lengths[i] = strLength;
i++;
position = k + 1;
positions = (int*)realloc(positions, (i+1) * sizeof(int));
lengths = (int*)realloc(lengths, (i+1) * sizeof(int));
positions[i] = position;
strLength = 0;
}else{
strLength++;
}
k++;
}
lengths[i] = strLength;
positions = (int*)realloc(positions, (i + 2) * sizeof(int));
positions[i+1] = lseek(findPtr, 0, SEEK_END);
for (int k = 0; k < i; k++)
{
printf("pos %d, len %d\n", positions[k], lengths[k]);
}
do{
printf("\nВведите номер строки\n");
scanf("%d", &num);
if(num > i+1){
printf("В файле всего %d строк(и), введите номер строки из этого диапазона\n", i+1);
}else if(num != 0){
strLength = lengths[num-1];
k = 0;
while(k < strLength){
printf("%c", findPtr[positions[num-1] + k]);
k++;
}
}
} while(num != 0);
return 0;
}