[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
Решения
Задача 1 - Таблица поиска строк в текстовом файле
lookup1.c
1 #include <sys/types.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 main(int argc, char *argv[])
9 {
10 long displ[500];
11 int fd, i = 1, j = 0, line_no, line_ln[500];
12 char c, buf[257];
13 static char err_msg[32] = "Input file - ";
14
15 if(( fd = open(argv[1], O_RDONLY)) == -1) {
16 perror(strcat(err_msg, argv[1]));
17 exit(1);
18 }
19
20 displ[1] = 0L;
21 while(read(fd, &c, 1))
22 if( c == '\n' ) {
23 j++;
24 line_ln[i++] = j;
25 displ[i] = lseek(fd, 0L, 1);
26 j = 0;
27 }
28 else
29 j++;
30
31 while( printf("Line number : ") && scanf("%d", &line_no)) {
32 if(line_no <= 0)
33 exit(0);
34 lseek(fd, displ[line_no], 0);
35 if(read(fd, buf, line_ln[line_no]))
36 write(1, buf, line_ln[line_no]);
37 else
38 fprintf(stderr, "Bad Line Number\n");
39 }
40 }
Задача 2 - Таблица поиска строк в текстовом файле
lookup2.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <string.h>
7
8 #define BUFSIZE 257
9
10 main(int argc, char *argv[])
11 {
12 long displ[500];
13 int fd1, fd2, i = 1, j = 0, line_no, line_ln[500];
14 char c, buf[BUFSIZE];
15 static char err_msg[32] = "Input file - ";
16
17 if ((fd1 = open("/dev/tty", O_RDONLY | O_NDELAY)) == -1) {
18 perror("/dev/tty");
19 exit(1);
20 }
21
22 if ((fd2 = open(argv[1], O_RDONLY)) == -1) {
23 perror(strcat(err_msg, argv[1]));
24 exit(1);
25 }
26
27 displ[1] = 0L;
28 while(read(fd2, &c, 1))
29 if( c == '\n' ) {
30 j++;
31 line_ln[i++] = j;
32 displ[i] = lseek(fd2, 0L, 1);
33 j = 0;
34 }
35 else
36 j++;
37
38 for (;;) {
39 printf("you have 5 seconds to enter a line number\n");
40 sleep(5);
41 if ((i = read(fd1, buf, BUFSIZE)) == 0) {
42 lseek(fd2, SEEK_SET, 0);
43 while((i = read(fd2, buf, BUFSIZE)) > 0)
44 write(1, buf, i);
45 exit(0);
46 }
47 else {
48 buf[i] = '\0';
49 line_no = atoi(buf);
50 if(line_no <= 0)
51 exit(0);
52 lseek(fd2, displ[line_no], 0);
53 if(read(fd2, buf, line_ln[line_no]))
54 write(1, buf, line_ln[line_no]);
55 else
56 fprintf(stderr, "Bad Line Number\n");
57 }
58 }
59 }
Задача 3 - Таблица поиска строк в текстовом файле
lookup3.c
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/mman.h>
8
9 main(int argc, char *argv[])
10 {
11 static char *displ[501];
12 char *p, buf[10];
13 int fd1, fd2, count, i = 1, j = 1, line_no, line_ln[500];
14 static char err_msg[32] = "Input file - ";
15 off_t size;
16
17 if ((fd1 = open("/dev/tty", O_RDONLY | O_NDELAY)) == -1) {
18 perror("/dev/tty");
19 exit(1);
20 }
21
22 if ((fd2 = open(argv[1], O_RDONLY)) == -1) {
23 perror(strcat(err_msg, argv[1]));
24 exit(1);
25 }
26
27 size = lseek(fd2, 0, SEEK_END);
28 p = mmap(0, size, PROT_READ, MAP_SHARED, fd2, 0);
29
30 displ[1] = p;
31 for(count = 0; count < size; count++)
32 if( *(p+count) == '\n' ) {
33 line_ln[i++] = j;
34 displ[i] = count+p+1;
35 j = 1;
36 }
37 else
38 j++;
39
40 displ[i] = 0;
41 for (;;) {
42 printf("you have 5 seconds to enter a line number\n");
43 sleep(5);
44 if ((i = read(fd1, buf, 10)) == 0) {
45 write(1, p, size);
46 exit(0);
47 }
48 else {
49 buf[i] = '\0';
50 line_no = atoi(buf);
51 if(line_no <= 0)
52 exit(0);
53 if(displ[line_no] != 0)
54 write(1, displ[line_no], line_ln[line_no]);
55 else
56 fprintf(stderr, "Bad Line Number\n");
57 }
58 }
59 }