Thursday, November 12, 2009

Chap12[C]g Reading text on console with pages

Write a program to display the contents of a text file on the screen. Make following provisions: Display the contents inside a box drawn with opposite corner co-ordinates being ( 0, 1 ) and ( 79, 23 ). Display the name of the file whose contents are being displayed, and the page numbers in the zeroth row. The moment one screenful of file has been displayed, flash a message ‘Press any key...’ in 24th row. When a key is hit, the next page’s contents should be displayed, and so on till the end of file.

Create a folder called "ctxt" in C drive and create a text file called page.txt in the ctxt folder.
Input lots of words into the page.txt file then run this program.

#include "stdio.h"
#include "dos.h"

void main()
{
void creategrid();
FILE *fp;
char ch,k;
int page=1,i,j,pgno,flag=0,count;


fp=fopen("c:\\ctxt\\page.txt","r");
while(1)
{
count=0;
clrscr();
creategrid();
for(i=3;i<=21&&flag!=1;i++)
{

for(j=4;j<=77&&flag!=1;j++)
{
ch=fgetc(fp);
if(ch==EOF)
{
flag=1;
break;
}
gotoxy(j,i);
printf("%c",ch);
count++;
}
}
gotoxy(38,24);
printf("Page %d",page);
if(page>1)
{
gotoxy(3,24);
printf("Key %c to go to page %d",17,page-1);
}
if(flag!=1)
{
gotoxy(58,24);
printf("Key %c to go to page %d",16,page+1);
}
if(flag==1)
{
gotoxy(66,24);
printf("Key %c to quit",16);
}
while(1)
{
pgno=getkey();
if(flag==1&&pgno==77)
break;
if(page==1&&pgno==75)
continue;

if(pgno==77&&flag!=1)
{
page+=1;
clrscr();
creategrid();
break;
}

if(pgno==75&&page!=1)
{
page-=1;
clrscr();
fseek(fp,-(1413+count),SEEK_CUR);
creategrid();
flag=0;
break;
}
}
if(flag==1&&pgno==77)
break;



}
fclose(fp);
}

getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}

void creategrid()
{
int i;
gotoxy(2,1);
printf("%c",218);
gotoxy(79,1);
printf("%c",191);
gotoxy(2,23);
printf("%c",192);
gotoxy(79,23);
printf("%c",217);

for(i=3;i<=78;i++)
{
gotoxy(i,1);
printf("%c",196);
gotoxy(i,23);
printf("%c",196);
}

for(i=2;i<=22;i++)
{
gotoxy(2,i);
printf("%c",179);
gotoxy(79,i);
printf("%c",179);
}
}