#include #include #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__) #define WIN #define CLEAR() system("cls") #include #else #define CLEAR() system("clear") #include #include #include #endif void Read_File(char *c, int max, FILE * ptr); //prototype int getMaxX(void) { #ifdef WIN CONSOLE_SCREEN_BUFFER_INFO csbi; if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) return (int) csbi.dwSize.X; else return 0; #else struct winsize size; if (ioctl(STDIN_FILENO,TIOCGWINSZ, (char*) &size)<0) return 0; else return size.ws_col; #endif } int main(int argc, char *argv[]) { int i, max; char * c; FILE *cfptr; cfptr = fopen(*(argv +1), "r"); max = getMaxX(); if(max == 0) { printf("Error while tryong to determine the width of the window.\n"); } else if (cfptr == NULL) { printf(" Error while opening the file\n"); } else { c = (char *) malloc(max*sizeof(char)); printf("File opened succesfully. Contents:\n\n"); Read_File(c, max, cfptr); } fclose(cfptr); return 0; } void Read_File(char *c, int max, FILE * ptr) { int i; int a=0; char user; long int * size= (long int *) malloc(sizeof(long int)); size[0] = 0; do { printf("\nPress N to continue. P to go backward\n"); user = getchar(); getchar(); if((user == 'N') || (user == 'n')) { CLEAR(); for (i = 0; (i < 20 )&& fgets (c, max, ptr) != NULL; i++ ) printf("%s", c); fflush(stdout); a++; if((a+1)>sizeof(a)/sizeof(int)) size = (long int*) realloc(size, sizeof(long int)*(a+1)); size[a]= ftell(ptr); } else if((user == 'P') || (user == 'p')) { if (a > 1) { a -= 2; CLEAR(); fseek(ptr, size[a], SEEK_SET); for (i = 0; (i < 20) && fgets (c, max, ptr) != NULL; i++) printf("%s", c); fflush(stdout); a++; } } else if((user == 'q') || (user == 'Q')) exit(0); }while(!feof(ptr)); }