Guest User

Untitled

a guest
Jul 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <strings.h>
  7. typedef struct pos
  8. {
  9. int abs_pos;
  10. int len;
  11. } pos;
  12. pos* allocate()
  13. {
  14. pos* ret = (pos*)malloc(sizeof(pos));
  15. return ret;
  16. }
  17. int search (char* str, char c ,int len)
  18. {
  19. int ret;
  20. for(int i = 0; i < len; i++)
  21. {
  22. if (str[i] == c)
  23. {
  24. //printf("%d\n", i);
  25. return i;
  26. }
  27.  
  28. }
  29. return -1;
  30.  
  31. }
  32.  
  33. int main(int argc, char** argv)
  34. {
  35. int j=0;
  36. int i = 0, k = 0, buff_size = 4, posv_len = 10,
  37. posv_num = 0, bytes_read, abs_pos = 0, str_pos = 0,end_pos = 0;
  38. char *buff = (char*)malloc(sizeof(char)*buff_size),*str_end;
  39. pos** posv = (pos**)malloc(sizeof(pos*)*posv_len);
  40. char path[50] = "/export/home/Admin/Desktop/test2";
  41. int file = open(path,O_RDONLY);
  42. if (file == -1)
  43. {
  44. printf("such file does not exist\n");
  45. return 0;
  46. }
  47. for(;;)
  48. {
  49. bytes_read = read(file,buff,buff_size);
  50. if ((bytes_read == 0))
  51. {
  52.  
  53. break;
  54. }
  55. if (i>posv_len-1)
  56. {
  57. posv_len = posv_len+10;
  58. posv = (pos**)realloc(posv,sizeof(pos*)*posv_len);
  59. }
  60.  
  61. for(;;)
  62. {
  63. j = search(&buff[str_pos+1],'\n', buff_size-str_pos);
  64. if (j == -1)
  65. {
  66. str_pos = 0;
  67. break;
  68.  
  69. }
  70. str_pos = str_pos + j + 1;
  71. posv[i] = allocate();
  72. posv[i]->abs_pos = k*buff_size+str_pos;
  73. posv[i]->len = k*buff_size+str_pos - abs_pos;
  74. abs_pos = abs_pos + k*buff_size+str_pos;
  75. i++;
  76. if (j == buff_size-1)
  77. {
  78. str_pos = 0;
  79. break;
  80. }
  81. }
  82. k++;
  83.  
  84. }
  85.  
  86. printf("%d\n", i);
  87. for (k=0;k<i;k++)
  88. {
  89. printf("%d\n", posv[k]->abs_pos);
  90. }
  91. //printf("%d <- posv[0]", posv[1]->len);
  92. close(file);
  93. return (EXIT_SUCCESS);
Add Comment
Please, Sign In to add comment