Chapter - 13: File Input-Output

Write a program to display the contents of a text file on the screen. Make the 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 the file.


File (f).txt

William Bradley "Brad" Pitt (born December 18, 1963) is an American actor and producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories and received three further Academy Award nominations, winning one, as producer under his own company Plan B Entertainment.

Pitt first gained recognition as a cowboy hitchhiker in the road movie Thelma & Louise (1991). His first leading roles in big-budget productions came with the dramas A River Runs Through It (1992) and Legends of the Fall (1994), and Interview with the Vampire (1994). He gave critically acclaimed performances in the crime thriller Seven and the science fiction film 12 Monkeys (both 1995),  the latter earning him a Golden Globe Award for Best Supporting Actor and an Academy Award nomination. Pitt starred in the cult film Fight Club (1999) and the major international hit Ocean's Eleven (2001) and its sequels, Ocean's Twelve (2004) and Ocean's Thirteen (2007). His greatest commercial successes have been  Troy (2004), Mr. & Mrs. Smith (2005), and World War Z (2013). Pitt received his second and third Academy Award nominations for his leading performances in The  Curious Case of Benjamin Button (2008) and Moneyball (2011). He produced The Departed (2006) and 12 Years a Slave (2013), both of which won the Academy  Award for Best Picture, and also The Tree of Life, Moneyball, and The Big Short (2015), all of which garnered Best Picture nominations.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<Windows.h>

void getkey();
void gotoxy(short, short);
void box(short, short, short, short);

int main()
{
	box(0, 1, 79, 23);
	FILE *fp;
	char str[85];
	int i, j = 1;
	fp = fopen("File (f).txt", "r");
	if (fp == NULL)
	{
		printf("FIle do not exist, or can't open the file.");
		exit(1);
	}
	i = 2;
	while (fgets(str, 76, fp) != NULL)
	{
		gotoxy(1, 0);
		/*Printing page number*/
		printf("%d", j);
		gotoxy(32, 0);
		/*Printing the file name.*/
		printf("File (f).txt");
		gotoxy(3, i);
		/*Printing the file's content.*/
		printf("%s", str);
		i++;
		if (i == 20)
		{
			gotoxy(32, i + 4);
			printf("Press any key...");
			getkey();
			system("cls");
			box(0, 1, 79, 23);
			/*Re-initializing variable for new page*/
			i = 2;
			/*Page number increamentation*/
			j++;
		}
	}
	getkey();
	system("cls");
	gotoxy(3, 10);
	printf("File Ends\n");
	_getch();
	return 0;
}

void getkey()
{
	char ch;
	ch = _getch();
	if (ch == 0)
		ch = _getch();
}
void gotoxy(short col, short row)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD position = { col,row };
	SetConsoleCursorPosition(h, position);
}
void box(short x1, short y1, short x2, short y2)
{
	int i;
	gotoxy(0, 1);
	printf("%c", 218);/*Making upper-left corener*/
	gotoxy(79, 1);
	printf("%c", 191);/*Making upper-right corner*/
	gotoxy(0, 23);
	printf("%c", 192);/*Making lower-left corner*/
	gotoxy(79, 23);
	printf("%c", 217);/*Making lower-right corner*/
	
	for (i = x1 + 1; i < x2; i++)
	{
		gotoxy(i, y1);
		printf("%c", 196);/*This will make upper side of the box*/
		gotoxy(i, y2);
		printf("%c", 196); /*This will make lower side of the box*/
	}

	for (i = y1 + 1; i < y2; i++)
	{
		gotoxy(x1, i);
		printf("%c", 179); //This will make left side of the box
		gotoxy(x2, i);
		printf("%c", 179); //This will make right side of the box
	}
}

© 2021 Garbage Valuegarbage value logo