Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!