Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SIZE_OF_BUF 50
- #include <stdio.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/time.h>
- void printLen(FILE *fin, char * buf, long int len, long int offset)
- {
- fseek(fin, offset, SEEK_SET);
- long int bufend = 0;
- long int sum = 0;
- int i;
- while (SIZE_OF_BUF + sum < len)
- {
- bufend = fread(buf, sizeof(char), SIZE_OF_BUF, fin);
- for(i = 0; i<bufend; i++)
- printf("%c", buf[i]);
- sum+=bufend;
- }
- fread(buf, sizeof(char), len - sum, fin);
- for(i = 0; i < len - sum; i++)
- printf("%c", buf[i]);
- printf("\n");
- }
- int main()
- {
- FILE *fin = fopen("lab5text", "r");
- if (fin == NULL)
- {
- perror("Can not open file");
- return 0;
- }
- char buf[SIZE_OF_BUF] = {0};
- long int posofnextl[100] = {0};
- int numofnextl = 0;
- long int curpos = 0;
- size_t bufend;
- while ((bufend = fread(&buf, sizeof(char), SIZE_OF_BUF, fin)) != 0)
- {
- int i = 0;
- for (; i < bufend; i++)
- {
- if ((buf[i] == '\n'))
- {
- posofnextl[numofnextl] = curpos + i;
- numofnextl++;
- }
- }
- if((bufend < SIZE_OF_BUF) && (buf[bufend - 1] != '\n'))
- {
- posofnextl[numofnextl] = curpos + i;
- numofnextl++;
- }
- curpos+=bufend;
- }
- if (numofnextl == 0)
- {
- printf("File is empty \n");
- return 0;
- }
- int i = 0;
- long int lenghts[100] = {0};
- lenghts[0] = posofnextl[0];
- for (i = 1; i < numofnextl; i++)
- {
- lenghts[i] = posofnextl[i] - posofnextl[i-1] - 1;
- }
- long int offsets[100] = {0};
- offsets[0] = 0;
- for(i = 1; i < numofnextl; i++)
- {
- offsets[i] = offsets[i-1] + lenghts[i-1] + 1;
- }
- int numberofstr = 0;
- fd_set input;
- int res;
- struct timeval timeout;
- FD_ZERO(&input);
- FD_SET(0, &input);
- timeout.tv_sec = 5;
- timeout.tv_usec = 0;
- res = select(1, &input, NULL, NULL, &timeout);
- if (res == 0) {
- printf("Не успел\n");
- for(i = 0; i < numofnextl; i++)
- {
- printLen(fin, buf, lenghts[i], offsets[i]);
- }
- return 0;
- }
- else
- {
- //успел
- }
- while (scanf("%d", &numberofstr))
- {
- if (numberofstr == 0)
- break;
- if((numberofstr<=numofnextl) && (numberofstr >0))
- {
- printLen(fin, buf, lenghts[numberofstr-1], offsets[numberofstr-1]);
- }
- else
- {
- printf("Wrong number of str\n");
- }
- }
- fclose(fin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement