forked from Oricadu/labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecondLab.c
More file actions
76 lines (53 loc) · 1.49 KB
/
secondLab.c
File metadata and controls
76 lines (53 loc) · 1.49 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, char const *argv[])
{
//FILE *file;
char name[] = "file.txt";
int strLength = 0, num, i = 0, position = 0;
int file1;
file1 = open(name, O_RDONLY);
int *lengths = NULL;
int *positions = NULL;
char buff[64];
lengths = (int*)malloc(1 * sizeof(int));
while(read(file1, buff, 1) != 0){
if (buff[0] == '\n')
{
lengths[i] = strLength;
i++;
position = lseek(file1, 0, SEEK_CUR);
positions = (int*)realloc(positions, (i+1) * sizeof(int));
lengths = (int*)realloc(lengths, (i+1) * sizeof(int));
positions[i] = position;
strLength = 0;
}else{
strLength++;
}
}
lengths[i] = strLength;
positions = (int*)realloc(positions, (i + 2) * sizeof(int));
positions[i+1] = lseek(file1, 0, SEEK_END);
do{
memset(buff, 0, 64);
printf("\nВведите номер строки\n");
scanf("%d", &num);
if(num > i+1){
printf("В файле всего %d строк(и), введите номер строки из этого диапазона\n", i+1);
}else if(num != 0){
lseek(file1, positions[num-1], SEEK_SET);
strLength = lengths[num-1];
read(file1, buff, strLength);
printf("\n%s, длина %d\n", buff, strLength);
}
} while(num != 0);
/* for (int n = 0; n < i; n++){
printf("\n%d, %d\n", positions[n], lengths[n]);
} */
close(file1);
free(positions);
free(lengths);
return 0;
}