daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest May 16th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. int numline(char *fp) {
  7.     char buff;
  8.     int coun = 0;
  9.     while ((buff = fgetc(fp)) != EOF) {
  10.         if (buff == '\n') {
  11.             ++coun;
  12.         }
  13.     }
  14.     return coun + 1;
  15. }
  16.  
  17. char missline(char *fp, int num) {
  18.     char buff;
  19.     int coun = 0;
  20.     while ((buff = fgetc(fp)) != EOF && coun < num) {
  21.         if (buff == '\n') {
  22.             ++coun;
  23.         }
  24.     }
  25.     return buff;
  26. }
  27.  
  28. int main() {
  29.  
  30.     FILE  *fp;
  31.     int counter = 0, num = 0, c = 0;
  32.     char *mode = "r";
  33.     char utility[10], name[10];
  34.  
  35.     while (counter == 0) {
  36.         printf(">");
  37.         scanf("%s %s", utility, name);
  38.         if ((c = getchar()) == ' ')
  39.             scanf("%d", &num);
  40.         if (strcmp(utility, "./tail") != 0) {
  41.             printf("Error: wrong utility\n");
  42.             if ((fp = fopen(name, mode)) == NULL)
  43.                 printf("Error: file isn't available\n");
  44.             if (c != '\n')
  45.                 while ((c = getchar()) != '\n');
  46.         }
  47.         else if ((fp = fopen(name, mode)) == NULL) {
  48.             printf("Error: file isn't available\n");
  49.             if (c != '\n')
  50.                 while ((c = getchar()) != '\n');
  51.         }
  52.         else
  53.             ++counter;
  54.     }
  55.     if (num == 0) {
  56.         counter = 0;
  57.         char arr[100];
  58.         int i = 0;
  59.         while ((arr[i] = fgetc(fp)) != EOF && counter < 5) {
  60.             if (arr[i] == '\n') {
  61.                 arr[i] = '\0';
  62.                 printf("%s\n", arr);
  63.                 i = 0;
  64.                 ++counter;
  65.             }
  66.             else i++;
  67.         }
  68.         arr[i] = '\0';
  69.         printf("%s", arr);
  70.     }
  71.     else {
  72.         char buff = 0;
  73.         counter = numline(fp);
  74.         fseek(fp, 0, SEEK_SET);
  75.         char arr[100];
  76.         int i = 0;
  77.         buff = missline(fp, counter - num);
  78.         counter = 0;
  79.         arr[i] = buff;
  80.         ++i;
  81.         while ((arr[i] = fgetc(fp)) != EOF) {
  82.             if (arr[i] == '\n') {
  83.                 arr[i] = '\0';
  84.                 printf("%s\n", arr);
  85.                 i = 0;
  86.             }
  87.             else i++;
  88.         }
  89.         arr[i] = '\0';
  90.         printf("%s\n", arr);
  91.     }
  92.     _getch();
  93.     fclose(fp);
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top