Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. #define SIZE_OF_BUF 50
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <sys/time.h>
  6. void printLen(FILE *fin, char * buf, long int len, long int offset)
  7. {
  8.     fseek(fin, offset, SEEK_SET);
  9.     long int bufend = 0;
  10.     long int sum = 0;
  11.     int i;
  12.     while (SIZE_OF_BUF + sum < len)
  13.     {
  14.         bufend = fread(buf, sizeof(char), SIZE_OF_BUF, fin);
  15.         for(i = 0; i<bufend; i++)
  16.             printf("%c", buf[i]);
  17.         sum+=bufend;
  18.  
  19.     }
  20.     fread(buf, sizeof(char), len - sum, fin);
  21.     for(i = 0; i < len - sum; i++)
  22.         printf("%c", buf[i]);
  23.     printf("\n");
  24. }
  25. int main()
  26. {
  27.     FILE *fin = fopen("lab5text", "r");
  28.    
  29.     if (fin == NULL)
  30.     {
  31.         perror("Can not open file");
  32.         return 0;
  33.     }
  34.  
  35.     char buf[SIZE_OF_BUF] = {0};
  36.     long int posofnextl[100] = {0};
  37.     int numofnextl = 0;
  38.     long int curpos = 0;
  39.  
  40.     size_t bufend;
  41.     while ((bufend = fread(&buf, sizeof(char), SIZE_OF_BUF, fin)) != 0)
  42.     {
  43.         int i = 0;
  44.         for (; i < bufend; i++)
  45.         {
  46.             if ((buf[i] == '\n'))
  47.             {
  48.                 posofnextl[numofnextl] = curpos + i;
  49.                 numofnextl++;
  50.             }  
  51.         }
  52.        
  53.         if((bufend < SIZE_OF_BUF) && (buf[bufend - 1] != '\n'))
  54.         {
  55.             posofnextl[numofnextl] = curpos + i;
  56.             numofnextl++;
  57.         }
  58.         curpos+=bufend;
  59.     }
  60.  
  61.     if (numofnextl == 0)
  62.     {
  63.         printf("File is empty \n");
  64.         return 0;
  65.     }
  66.  
  67.     int i = 0;
  68.     long int lenghts[100] = {0};
  69.     lenghts[0] = posofnextl[0];
  70.     for (i = 1; i < numofnextl; i++)
  71.     {
  72.         lenghts[i] = posofnextl[i] - posofnextl[i-1] - 1;
  73.     }
  74.  
  75.     long int offsets[100] = {0};
  76.     offsets[0] = 0;
  77.     for(i = 1; i < numofnextl; i++)
  78.     {
  79.         offsets[i] = offsets[i-1] + lenghts[i-1] + 1;
  80.     }
  81.  
  82.     int numberofstr = 0;
  83.  
  84.     fd_set input;
  85.     int res;
  86.     struct timeval timeout;
  87.     FD_ZERO(&input);
  88.     FD_SET(0, &input);
  89.     timeout.tv_sec = 5;
  90.     timeout.tv_usec = 0;
  91.  
  92.     res = select(1, &input, NULL, NULL, &timeout); 
  93.  
  94.     if (res == 0) {
  95.         printf("Не успел\n");
  96.         for(i = 0; i < numofnextl; i++)
  97.         {
  98.             printLen(fin, buf, lenghts[i], offsets[i]);
  99.         }
  100.         return 0;
  101.     }
  102.     else
  103.     {
  104.         //успел
  105.     }
  106.  
  107.     while (scanf("%d", &numberofstr))
  108.     {
  109.         if (numberofstr == 0)
  110.             break;
  111.         if((numberofstr<=numofnextl) && (numberofstr >0))
  112.         {
  113.             printLen(fin, buf, lenghts[numberofstr-1], offsets[numberofstr-1]);
  114.         }
  115.         else
  116.         {
  117.             printf("Wrong number of str\n");
  118.         }
  119.     }
  120.    
  121.  
  122.     fclose(fin);
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement