Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define LEN 300
- #define STR 120
- int main(int argc, char *argv[]) {
- int N=10,num=0,i;
- char str[STR][LEN];
- if(argc==2 && atoi(argv[1])!=0) N=atoi(argv[1]);
- else N=10;
- if (argc==1 || N!=10){
- while(fgets(str[num],LEN,stdin)!=NULL) num++;
- if(N<num){
- printf("Last %d strings:\n",N);
- for(i=num-N;i<num;i++) printf("%s",str[i]);
- }
- else{
- printf("All strings:\n");
- for (i=0;i<num;i++) printf("%s",str[i]);
- }
- }
- if(argc==2 && atoi(argv[1])==0){
- N=10;
- FILE *fp;
- if((fp=fopen(argv[1],"r"))!=NULL){
- while(!feof(fp)){
- fgets(str[num],LEN,fp);
- num++;
- }
- }
- else {
- printf("Failed to open the file\n");
- exit(0);
- }
- if(N<num){
- printf("Last %d strings\n",N);
- for(i=num-N-1;i<num;i++){
- printf("%s",str[i]);
- }
- }
- else{
- printf("All strings\n");
- for(i=0;i<num;i++){
- printf("%s",str[i]);
- }
- }
- fclose(fp);
- }
- if(argc==3){
- N=atoi(argv[1]);
- FILE *fp;
- if((fp=fopen(argv[2],"r"))!=NULL){
- while(!feof(fp)){
- fgets(str[num],LEN,fp);
- num++;
- }
- }
- else {
- printf("Failed to open the file\n");
- exit(0);
- }
- if(N<num){
- printf("Last %d strings\n",N);
- for(i=num-N-1;i<num;i++){
- printf("%s",str[i]);
- }
- }
- else{
- printf("All strings\n");
- for(i=0;i<num;i++){
- printf("%s",str[i]);
- }
- }
- fclose(fp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement