Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <ctype.h>
- #define INIT_COUNT 100
- typedef struct TableElement
- {
- int offset;
- int length;
- } TableElement;
- int make_table(char *name, TableElement *table, int *line_count, int *max_count)
- {
- int handle = open(name, O_RDONLY); //возвращает описатель файла, подаётся путь и флаги через OR (|)
- if (handle == -1) //в errno устанавливается значение
- {
- perror("Cannot open file\n"); //выводится ошибка, код которой в errno
- exit(-1);
- }
- int offset = 0, line_length = 0;
- char symbol;
- ssize_t err_code = 0;
- while ((err_code = read(handle, &symbol, 1)) > 0) //возвращает число считанных байт
- {
- ++offset;
- ++line_length;
- if (symbol == '\n')
- {
- table[*line_count + 1].offset = offset;
- table[*line_count].length = line_length;
- line_length = 0;
- ++*line_count;
- if (*line_count >= *max_count)
- {
- table = realloc(table, (sizeof(TableElement) * (*max_count+128)));
- *max_count += 128;
- }
- }
- }
- if (err_code == -1)
- {
- perror("Cannot read file");
- exit(-1);
- }
- if (symbol != '\n')
- {
- table[*line_count + 1].offset = offset;
- table[*line_count].length = line_length;
- ++*line_count;
- }
- close(handle);
- return 0;
- }
- int print_string(char *name, TableElement *table, int line_count)
- {
- char *buffer = (char *) malloc(1024);
- int handle = open(name, O_RDONLY);
- if (handle == -1)
- {
- perror("Cannot open file\n");
- exit(-1);
- }
- printf("Enter line numbers: ");
- while (1)
- {
- //printf("Enter line number: ");
- char number_c[1024];
- int n = scanf("%s", &number_c);
- int is_number = 1;
- for (char *str = number_c; *str != '\0'; str++)
- {
- if (!(isdigit((int) *str)))
- {
- is_number = 0;
- }
- }
- if (is_number == 0)
- {
- printf("(%s) ! Bad input\n\n", number_c);
- continue;
- }
- int number = atoi(number_c);
- if (n <= 0)
- {
- printf("Bad input\n\n");
- continue;
- }
- if (number == 0)
- break;
- if ((number < 0) || (number > line_count))
- {
- printf("(%s) ! Line number must be < %d\n\n", number_c, line_count);
- continue;
- }
- if (lseek(handle, table[number - 1].offset, SEEK_SET) == -1)
- {
- perror("Cannot read file");
- exit(-1);
- }
- if (read(handle, buffer, table[number - 1].length) == -1)
- {
- perror("Cannot read file");
- exit(-1);
- }
- buffer[table[number - 1].length] = '\0';
- printf("(%d) : %s\n", number, buffer);
- }
- close(handle);
- free(buffer);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- printf("Expected: ./prog input.txt\n");
- exit(-1);
- }
- int line_count = 0;
- int max_count = 128;
- TableElement *table = (TableElement *) malloc(sizeof(TableElement) * max_count);
- make_table(argv[1], table, &line_count, &max_count);
- print_string(argv[1], table, line_count);
- free(table);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement