Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <strings.h>
- typedef struct pos
- {
- int abs_pos;
- int len;
- } pos;
- pos* allocate()
- {
- pos* ret = (pos*)malloc(sizeof(pos));
- return ret;
- }
- int search (char* str, char c ,int len)
- {
- int ret;
- for(int i = 0; i < len; i++)
- {
- if (str[i] == c)
- {
- //printf("%d\n", i);
- return i;
- }
- }
- return -1;
- }
- int main(int argc, char** argv)
- {
- int j=0;
- int i = 0, k = 0, buff_size = 4, posv_len = 10,
- posv_num = 0, bytes_read, abs_pos = 0, str_pos = 0,end_pos = 0;
- char *buff = (char*)malloc(sizeof(char)*buff_size),*str_end;
- pos** posv = (pos**)malloc(sizeof(pos*)*posv_len);
- char path[50] = "/export/home/Admin/Desktop/test2";
- int file = open(path,O_RDONLY);
- if (file == -1)
- {
- printf("such file does not exist\n");
- return 0;
- }
- for(;;)
- {
- bytes_read = read(file,buff,buff_size);
- if ((bytes_read == 0))
- {
- break;
- }
- if (i>posv_len-1)
- {
- posv_len = posv_len+10;
- posv = (pos**)realloc(posv,sizeof(pos*)*posv_len);
- }
- for(;;)
- {
- j = search(&buff[str_pos+1],'\n', buff_size-str_pos);
- if (j == -1)
- {
- str_pos = 0;
- break;
- }
- str_pos = str_pos + j + 1;
- posv[i] = allocate();
- posv[i]->abs_pos = k*buff_size+str_pos;
- posv[i]->len = k*buff_size+str_pos - abs_pos;
- abs_pos = abs_pos + k*buff_size+str_pos;
- i++;
- if (j == buff_size-1)
- {
- str_pos = 0;
- break;
- }
- }
- k++;
- }
- printf("%d\n", i);
- for (k=0;k<i;k++)
- {
- printf("%d\n", posv[k]->abs_pos);
- }
- //printf("%d <- posv[0]", posv[1]->len);
- close(file);
- return (EXIT_SUCCESS);
Add Comment
Please, Sign In to add comment