Advertisement
Extremum

ex43

May 4th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define LEN 300
  4. #define STR 120
  5.  
  6. int main(int argc, char *argv[]) {
  7.   int N=10,num=0,i;
  8.   char str[STR][LEN];
  9.   if(argc==2 && atoi(argv[1])!=0) N=atoi(argv[1]);
  10.   else N=10;
  11.   if (argc==1 || N!=10){
  12.         while(fgets(str[num],LEN,stdin)!=NULL) num++;
  13.      if(N<num){
  14.        printf("Last %d strings:\n",N);
  15.         for(i=num-N;i<num;i++) printf("%s",str[i]);
  16.      }
  17.      else{
  18.        printf("All strings:\n");
  19.        for (i=0;i<num;i++) printf("%s",str[i]);
  20.      }
  21.   }
  22.   if(argc==2 && atoi(argv[1])==0){
  23.     N=10;
  24.      FILE *fp;
  25.      if((fp=fopen(argv[1],"r"))!=NULL){
  26.        while(!feof(fp)){
  27.         fgets(str[num],LEN,fp);
  28.          num++;
  29.        }                
  30.      }
  31.      else {
  32.         printf("Failed to open the file\n");
  33.         exit(0);
  34.      }
  35.         if(N<num){
  36.           printf("Last %d strings\n",N);
  37.            for(i=num-N-1;i<num;i++){
  38.             printf("%s",str[i]);
  39.            }
  40.         }
  41.         else{
  42.             printf("All strings\n");
  43.             for(i=0;i<num;i++){
  44.             printf("%s",str[i]);
  45.            }
  46.         }
  47.       fclose(fp);
  48.   }
  49.   if(argc==3){
  50.         N=atoi(argv[1]);
  51.      FILE *fp;
  52.      if((fp=fopen(argv[2],"r"))!=NULL){
  53.        while(!feof(fp)){
  54.         fgets(str[num],LEN,fp);
  55.          num++;
  56.        }                
  57.      }
  58.      else {
  59.         printf("Failed to open the file\n");
  60.         exit(0);
  61.      }
  62.         if(N<num){
  63.           printf("Last %d strings\n",N);
  64.            for(i=num-N-1;i<num;i++){
  65.             printf("%s",str[i]);
  66.            }
  67.         }
  68.         else{
  69.             printf("All strings\n");
  70.             for(i=0;i<num;i++){
  71.             printf("%s",str[i]);
  72.            }
  73.         }
  74.       fclose(fp);
  75.   }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement