Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 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.  
  8. #define LINES_COUNT 100
  9.  
  10. typedef struct TableElement
  11. {
  12. int offset;
  13. int length;
  14. } TableElement;
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. if (argc != 2)
  19. {
  20. printf("Bad input, expected: ./prog input.txt\n");
  21. exit(-1);
  22. }
  23.  
  24. TableElement *table = (TableElement *) malloc(sizeof(TableElement) * LINES_COUNT);
  25.  
  26. int handle = open(argv[1], O_RDONLY); //возвращает описатель файла, подаётся путь и флаги через OR (|)
  27.  
  28. if (handle == -1) //в errno устанавливается значение
  29. {
  30. perror("Cannot open file\n"); //выводится ошибка, код которой в errno
  31. exit(-1);
  32. }
  33.  
  34. int offset = 0, line_length = 0, line_count = 0;
  35. char symbol;
  36. ssize_t err_code = 0;
  37.  
  38. while ((err_code = read(handle, &symbol, 1)) > 0) //возвращает число считанных байт
  39. {
  40. ++offset;
  41. ++line_length;
  42.  
  43. if (symbol == '\n')
  44. {
  45. table[line_count + 1].offset = offset;
  46. table[line_count].length = line_length;
  47. line_length = 0;
  48. ++line_count;
  49.  
  50. if (line_count >= LINES_COUNT)
  51. {
  52. fprintf(stderr, "Exceeding max file size\n");
  53. exit(-1);
  54. }
  55. }
  56. }
  57.  
  58. if (err_code == -1)
  59. {
  60. perror("Cannot read file");
  61. exit(-1);
  62. }
  63.  
  64. if (symbol != '\n')
  65. {
  66. table[line_count + 1].offset = offset;
  67. table[line_count].length = line_length;
  68. ++line_count;
  69. }
  70.  
  71. if (lseek(handle, 0, SEEK_SET) == -1) //устанавливает указатель на смещение 0 от начала файла
  72. {
  73. perror("Cannot read file");
  74. exit(-1);
  75. }
  76. char *buffer = (char *) malloc(1024);
  77.  
  78. while (1)
  79. {
  80. printf("Enter line number: ");
  81. int number;
  82. int n = scanf("%d", &number);
  83.  
  84. if (n <= 0)
  85. {
  86. printf("Bad input\n");
  87. fflush(stdin);
  88. continue;
  89. }
  90.  
  91. if (number == 0)
  92. break;
  93.  
  94. if ((number < 0) || (number > line_count))
  95. {
  96. printf("Bad string number\n");
  97. continue;
  98. }
  99.  
  100.  
  101. if (lseek(handle, table[number - 1].offset, SEEK_SET) == -1)
  102. {
  103. perror("Cannot read file");
  104. exit(-1);
  105. }
  106.  
  107. if (read(handle, buffer, table[number - 1].length) == -1)
  108. {
  109. perror("Cannot read file");
  110. exit(-1);
  111. }
  112. buffer[table[number - 1].length] = '\0';
  113.  
  114. printf("%s\n", buffer);
  115. fflush(stdin);
  116. }
  117.  
  118. free(table);
  119. free(buffer);
  120. close(handle);
  121.  
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement