All pastes #403225 Raw Edit

Miscellany

public c v1 · immutable
·published 2007-03-20 07:17 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)#define WIN#define CLEAR() System("cls")#include <windows.h>#else#define CLEAR() system("clear")#include <termios.h> #include <sys/ioctl.h>#include <unistd.h>#endif#define MAX getMaxX()  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 getMaxY(void){        #ifdef WIN        int maxY;        CONSOLE_SCREEN_BUFFER_INFO csbi;        if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))                return (int) csbi.dwSize.Y;        else           return 0;        #else        struct winsize size;        if (ioctl(STDIN_FILENO,TIOCGWINSZ, (char*) &size)<0)                 return 0;        else                return size.ws_row;        #endif}  int main(int argc, char *argv[]){   int i;  char c[MAX];  FILE *cfptr;  printf("width = %d\nheight = %d\n", getMaxX(), getMaxY());  cfptr = fopen(*(argv +1), "rb");   if (cfptr == NULL)  {  	printf(" Error while opening the file\n");  }  else  {  	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;  while (fgets(c, max,ptr) != NULL)  {             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++; 				size = (long int*) realloc(size, sizeof(long int)*(a+1));                size[a]= ftell(ptr);             }            else if((user == 'P') || (user == 'p'))            { 				printf("a = %d\n", a);                 if (a > 0)                 {                 	 a--;                 	 CLEAR();                     fseek(ptr, size[a], SEEK_SET);                     for (i = 0; (i < 20) && fgets (c, MAX, ptr) != NULL; i++)                     	printf("%s", c);                     fflush(stdout);                 }              }              else if((user == 'q') || (user == 'Q'))              	exit(0);    }}