Guest User

Untitled

a guest
Jul 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <poll.h>
  9.  
  10. #define BUF_SIZE 32
  11.  
  12. int main(int argc, char **argv) {
  13. int file = 0;
  14. ssize_t bytesRead = 0;
  15. void *buf[BUF_SIZE];
  16. int *stringStart;
  17. int *stringLength;
  18. int i = 0;
  19. int stringNumber = 1; // Nomer stroki
  20. int absolutePosition = -1; // Pozitsiya v fajle
  21. char *s;
  22. struct pollfd fds[1];
  23. char c;
  24.  
  25. fds[0].fd = 0; // stdin
  26. fds[0].events = POLLIN;
  27. fds[0].revents = 0;
  28.  
  29. stringStart = (int *)malloc(sizeof(int)*2);
  30. stringLength = (int *)malloc(sizeof(int)*2);
  31. stringStart[0] = 0; // Fiktivnaya 0-aya stroka
  32. stringStart[1] = 0; // Stroki nachinayutsya s 1
  33. stringLength[0] = 0;
  34. stringLength[1] = 0;
  35.  
  36. // Open file
  37. if (argv[1] == NULL) {
  38. printf("Ne ukazan fajl\n");
  39. return -1;
  40. }
  41. file = open(argv[1], O_RDONLY);
  42. if (file == -1) {
  43. printf("Ne udalos' otkryt' fajl\n");
  44. return -1;
  45. }
  46.  
  47. // Read, make table
  48. while((bytesRead = read(file, buf, (size_t) BUF_SIZE)) > 0) {
  49. for(i = 0; i < bytesRead; i++) {
  50. absolutePosition++;
  51. // printf("%c", ((char *)buf)[i]);
  52. if(((char *)buf)[i] == '\n') {
  53. stringLength[stringNumber] = absolutePosition - stringStart[stringNumber];
  54. stringNumber++;
  55. stringStart = (int *) realloc(stringStart, sizeof(int)*(stringNumber + 1));
  56. stringLength = (int *) realloc(stringLength, sizeof(int)*(stringNumber + 1));
  57. stringStart[stringNumber] = absolutePosition + 1;
  58. }
  59. }
  60. }
  61. // stringNumber--;
  62. if (absolutePosition == -1) {
  63. printf("Razmer fajla 0\n");
  64. close(file);
  65. return 0;
  66. }
  67.  
  68. /*
  69. // Print table
  70. for(i = 1; i < stringNumber; i++)
  71. printf("%d : %d : %d\n", i, stringStart[i], stringLength[i]);
  72. */
  73.  
  74. /******************************************************
  75. ******************************************************/
  76. while(poll(fds, 1, 5000))
  77. if((i = atoi(gets(s))) > 0) {
  78. if(i < stringNumber) {
  79. char buf[stringLength[i] + 1];
  80. buf[stringLength[i]] = 0;
  81. lseek(file, (off_t)stringStart[i], SEEK_SET);
  82. read(file, buf, (size_t)stringLength[i]);
  83. printf("%s\n", buf);
  84. } else printf("Takoj stroki net\n");
  85. fds[0].revents = 0;
  86. }
  87. else {
  88. close(file);
  89. free(stringLength);
  90. free(stringStart);
  91. return 0;
  92. }
  93.  
  94. // Vyvod vsego fajla
  95. for(i = 1; i < stringNumber; i++) {
  96. char buf[stringLength[i] + 1];
  97. buf[stringLength[i]] = 0;
  98. lseek(file, (off_t)stringStart[i], SEEK_SET);
  99. read(file, buf, (size_t)stringLength[i]);
  100. printf("%s\n", buf);
  101. }
  102.  
  103. /******************************************************
  104. ******************************************************/
  105.  
  106. // Finish
  107. close(file);
  108. free(stringLength);
  109. free(stringStart);
  110. return 0;
  111. }
Add Comment
Please, Sign In to add comment