Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define LEN 256
  7. #define COL 128
  8.  
  9.  
  10. int main(int argc, char *argv[]) {
  11.     int N = 10;
  12.     char text[COL][LEN];
  13.  
  14.     //когда нет аргументов
  15.     //пример gcc -o main main.c && ./main
  16.     if (argc == 1){
  17.         int n = 0;
  18.         printf("Введите строки. Чтобы закончить ввод, введите строку stop\n");
  19.         do{
  20.             scanf("%s", text[n]);
  21.             n++;
  22.         }while (strcmp(text[n - 1], "stop"));
  23.         printf("Последние 10 строк:\n");
  24.         if (n-1 > N){
  25.             for (int i = n-1; i >= n-1-N ;i--){
  26.                 printf("%s\n", text[i]);
  27.             }
  28.         } else{
  29.             for (int i = 0; i < n - 1; i++) {
  30.                 printf("%s\n", text[i]);
  31.             }
  32.         }
  33.     }
  34.  
  35.     //когда два аргумента: количество строк и имя файла
  36.     //пример gcc -o main main.c && ./main 3 my_file.txt
  37.     if (argc == 3) {
  38.         N = atoi(argv[1]);
  39.         FILE *file;
  40.         file = fopen(argv[2], "r");
  41.         if (file == NULL){
  42.             printf("Не открыли файл\n");
  43.             return 1;
  44.         }else {
  45.             int n = 0;
  46.             printf("Oткрыли файл\n");
  47.             while (!feof(file)) {
  48.                 fgets(text[n], LEN, file);
  49.                 n++;
  50.             }
  51.             printf("Последние N строк:\n");
  52.             if (n - 1 > N) {
  53.                 for (int i = n - 1; i >= n - 1 - N; i--) {
  54.                     printf("%s", text[i]);
  55.                 }
  56.             } else {
  57.                 for (int i = 0; i < n - 1; i++) {
  58.                     printf("%s", text[i]);
  59.                 }
  60.             }
  61.         }
  62.         fclose(file);
  63.     }
  64.  
  65.     //когда один аргумента - имя файла
  66.     //пример gcc -o main main.c && ./main my_file.txt
  67.     if (argc == 2){
  68.         FILE *file;
  69.         file = fopen(argv[1], "r");
  70.         if (file == NULL){
  71.             printf("Не открыли файл\n");
  72.             return 1;
  73.         }else {
  74.             int n = 0;
  75.             printf("Oткрыли файл\n");
  76.             while (!feof(file)) {
  77.                 fgets(text[n], LEN, file);
  78.                 n++;
  79.             }
  80.             printf("Последние N строк:\n");
  81.             if (n - 1 > N) {
  82.                 for (int i = n - 1; i >= n - 1 - N; i--) {
  83.                     printf("%s", text[i]);
  84.                 }
  85.             } else {
  86.                 for (int i = 0; i < n - 1; i++) {
  87.                     printf("%s", text[i]);
  88.                 }
  89.             }
  90.         }
  91.         fclose(file);
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement