Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8.  
  9. #define INIT_COUNT 100
  10.  
  11. typedef struct TableElement
  12. {
  13. int offset;
  14. int length;
  15. } TableElement;
  16.  
  17. int make_table(char *name, TableElement *table, int *line_count, int *max_count)
  18. {
  19. int handle = open(name, O_RDONLY); //возвращает описатель файла, подаётся путь и флаги через OR (|)
  20.  
  21. if (handle == -1) //в errno устанавливается значение
  22. {
  23. perror("Cannot open file\n"); //выводится ошибка, код которой в errno
  24. exit(-1);
  25. }
  26.  
  27. int offset = 0, line_length = 0;
  28. char symbol;
  29. ssize_t err_code = 0;
  30.  
  31. while ((err_code = read(handle, &symbol, 1)) > 0) //возвращает число считанных байт
  32. {
  33. ++offset;
  34. ++line_length;
  35.  
  36. if (symbol == '\n')
  37. {
  38. table[*line_count + 1].offset = offset;
  39. table[*line_count].length = line_length;
  40. line_length = 0;
  41. ++*line_count;
  42.  
  43. if (*line_count >= *max_count)
  44. {
  45. table = realloc(table, (sizeof(TableElement) * (*max_count+128)));
  46. *max_count += 128;
  47. }
  48. }
  49. }
  50.  
  51. if (err_code == -1)
  52. {
  53. perror("Cannot read file");
  54. exit(-1);
  55. }
  56.  
  57. if (symbol != '\n')
  58. {
  59. table[*line_count + 1].offset = offset;
  60. table[*line_count].length = line_length;
  61. ++*line_count;
  62. }
  63.  
  64. close(handle);
  65.  
  66. return 0;
  67. }
  68.  
  69. int print_string(char *name, TableElement *table, int line_count)
  70. {
  71. char *buffer = (char *) malloc(1024);
  72.  
  73. int handle = open(name, O_RDONLY);
  74.  
  75. if (handle == -1)
  76. {
  77. perror("Cannot open file\n");
  78. exit(-1);
  79. }
  80.  
  81. printf("Enter line numbers: ");
  82.  
  83. while (1)
  84. {
  85. //printf("Enter line number: ");
  86. char number_c[1024];
  87. int n = scanf("%s", &number_c);
  88.  
  89. int is_number = 1;
  90. for (char *str = number_c; *str != '\0'; str++)
  91. {
  92. if (!(isdigit((int) *str)))
  93. {
  94. is_number = 0;
  95. }
  96. }
  97.  
  98. if (is_number == 0)
  99. {
  100. printf("(%s) ! Bad input\n\n", number_c);
  101. continue;
  102. }
  103.  
  104. int number = atoi(number_c);
  105.  
  106. if (n <= 0)
  107. {
  108. printf("Bad input\n\n");
  109. continue;
  110. }
  111.  
  112. if (number == 0)
  113. break;
  114.  
  115. if ((number < 0) || (number > line_count))
  116. {
  117. printf("(%s) ! Line number must be < %d\n\n", number_c, line_count);
  118. continue;
  119. }
  120.  
  121. if (lseek(handle, table[number - 1].offset, SEEK_SET) == -1)
  122. {
  123. perror("Cannot read file");
  124. exit(-1);
  125. }
  126.  
  127. if (read(handle, buffer, table[number - 1].length) == -1)
  128. {
  129. perror("Cannot read file");
  130. exit(-1);
  131. }
  132. buffer[table[number - 1].length] = '\0';
  133.  
  134. printf("(%d) : %s\n", number, buffer);
  135. }
  136.  
  137. close(handle);
  138. free(buffer);
  139.  
  140. return 0;
  141. }
  142.  
  143. int main(int argc, char *argv[])
  144. {
  145. if (argc != 2)
  146. {
  147. printf("Expected: ./prog input.txt\n");
  148. exit(-1);
  149. }
  150.  
  151. int line_count = 0;
  152. int max_count = 128;
  153.  
  154. TableElement *table = (TableElement *) malloc(sizeof(TableElement) * max_count);
  155.  
  156. make_table(argv[1], table, &line_count, &max_count);
  157. print_string(argv[1], table, line_count);
  158.  
  159. free(table);
  160.  
  161. return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement