Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C++ program that displays the contents of a file in curses mode. If the contents of the file are too big to fit

Write a C++ program that displays the contents of a file in curses mode. If the contents of the file are too big to fit on the screen, then your program needs to allow the user to scroll through the output using the up and down arrow keys.

Running program

Assuming your executable is called program1, you should be able to run your program as follows to open and display the contents of a file called filename.txt

$ ./program1 filename.txt

For this program, you will need to use the following prototype for main:

int main(const int argc, const char * argv []);

code below is what i have so far, which is suppose to display filename.txt and be able to scroll the output with the arrow keys. But my code does not let the filename.txt to display nor allow to scroll up and down.

Please help!

#include #include #include

using namespace std;

WINDOW *create_newwin(int height, int width); void destroy_win(WINDOW *local_win);

int main (const int argc, char *argv[] ) {

WINDOW *my_win; int width, height; int ch;

initscr(); cbreak(); keypad(stdscr, TRUE);

height = 3; width = 10; refresh(); my_win = create_newwin(height, width);

while((ch = getch()) != KEY_F(1)) { switch(ch) { case KEY_UP: destroy_win(my_win); my_win = create_newwin(height-, width); break; case KEY_DOWN: destroy_win(my_win); my_win = create_newwin(height++ , width); break; } }

if ( argc != 2 ) // argc should be 2 for correct execution // We print argv[0] assuming it is the program name cout<<"usage: "<< argv[0] <<" "; else { // We assume argv[1] is a filename to open ifstream the_file ( argv[1] ); // Always check to see if file opening succeeded if ( !the_file.is_open() ) cout<<"Could not open file "; else { char x; // the_file.get ( x ) returns false if the end of the file // is reached or an error occurs while ( the_file.get ( x ) ) cout<< x; } // the_file is closed implicitly here }

endwin();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Select suitable tools to analyze service problems.

Answered: 1 week ago