Im trying to get this functionality with this C program If more than one file is given, and if any of the files, except the
Im trying to get this functionality with this C program
If more than one file is given, and if any of the files, except the last one, does not exist, the program exits without viewing the rest of the files.
so say the compiled program is called mymore if i type mymore 1.txt 2.txt 3.txt
it should view the text file contents one after the other
/* * mymore.c -- simplified version of Linux more command * This command read a file and print the first 24 lines. * Then, the program pause for a few special commands to continue or quit. */ #include #include #define PAGELEN 24 /* macro definition, allow constant values to be declared for use throughout your code */ #define LINELEN 512 void do_more(FILE *); /* Forward declaration for a function. It tells the compiler you're going to define the function later. The function could be in a different file. Forward declaration let you use the function before it is defined */ int see_more(); /* The main function */ int main (int ac, char *av[]) { FILE *fp; /* A file pointer with variable name of fp */ if( ac == 1 ) do_more(stdin); else while( --ac ) { if( (fp=fopen( *++av, "r")) != NULL) { do_more (fp); fclose(fp); } else exit(1); // exit, 1 means something wrong } return 0; // 0 means every OK } /* read PAGELEN lines, then call see_more() for further instructions */ void do_more(FILE *fp) { char line[LINELEN]; // an array int num_of_lines = 0; int reply; while( fgets(line, LINELEN, fp) ) { if( num_of_lines == PAGELEN ) { reply = see_more(); if(reply == 0){ break; } num_of_lines -= reply; } if( fputs(line, stdout) == EOF ) // EOF: End of File { exit(1); } num_of_lines++; } } /* print message, wait for response, return # of lines to advance q: quit, space: next page, enter key: next line */ int see_more() { int c; printf("\033[7m more? \033[m"); // reverse the monitor while( (c=getchar()) != EOF) // get response { if(c=='q') return 0; if(c==' ') return PAGELEN; if(c==' ') return 1; } return 0; }