Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Explain each line of code. #include <ncurses.h> #include <stdlib.h> int main(int

ID: 3738474 • Letter: E

Question

Explain each line of code.

#include <ncurses.h>

#include <stdlib.h>

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

{

int ch, prev, row, col;

prev = EOF;

FILE *fp;

int y, x;

if(argc != 2)

{

printf("Usage: %s <a c file name> ", argv[0]);

exit(1);

}

fp = fopen(argv[1], "r");

if(fp == NULL)

{

perror("Cannot open input file");

exit(1);

}

initscr();

getmaxyx(stdscr, row, col);

while((ch = fgetc(fp)) != EOF) {

getyx(stdscr, y, x);

if(y == (row - 1))

{

printw("<-Press Any Key->");

getch();

clear(); /* clear the screen */

move(0, 0); /* start at the beginning of the screen */

}

if(prev == '/' && ch == '*') /* If it is / and * then only

* switch bold on */   

{

attron(A_BOLD);

getyx(stdscr, y, x);

move(y, x - 1);

printw("%c%c", '/', ch);

}

else

printw("%c", ch);

refresh();

if(prev == '*' && ch == '/')

attroff(A_BOLD);   

prev = ch;

}

endwin(); /* End curses mode */

fclose(fp);

return 0;

}

Explanation / Answer

#include <ncurses.h>

#include <stdlib.h>

// main which will take file name as a command line argument

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

{

                // variable declarations

                int ch, prev, row, col;

                prev = EOF;

                // pointer for file operations

                FILE *fp;

                // variables used for position on screen

                int y, x;

                // check whether command line argumanets contains file name or not

                if(argc != 2)

                {

                                // if not then give message to user

                                printf("Usage: %s <a c file name> ", argv[0]);

                                // exit from the program

                                exit(1);

                }

                // Open inputted file

                fp = fopen(argv[1], "r");

                // Check file is open or not

                if(fp == NULL)

                {

                                // if not open then give message to user

                                perror("Cannot open input file");

                                // exit from the program

                                exit(1);

                }

                // initialize the ncurses data structures and to read the proper terminfo file

                initscr();

                // macros that store the current beginning coordinates and size of the specified window

                getmaxyx(stdscr, row, col);

                // read characters from the file until it reach on end of the file

                while((ch = fgetc(fp)) != EOF)   {

                                // places the current cursor position of the given window in the two integer variables y and x

                                getyx(stdscr, y, x);

                                // check whether y equals (row - 1)

                                if(y == (row - 1))

                                {

                                                // give message to user

                                                printw("<-Press Any Key->");

                                                // wait for some time until a key is hit       

                                                getch();

                                                // clear the screen

                                                clear();

                                                // start at the beginning of the screen

                                                move(0, 0);

                                }

                                // If it is / and * then only switch bold on

                                if(prev == '/' && ch == '*')     

                                {

                                                // manipulate the highlights (Switch bold on)

                                                attron(A_BOLD);

                                                // places the current cursor position of the given window in the two integer variables y and x

                                                getyx(stdscr, y, x);

                                                // x, y position of the screen

                                                move(y, x - 1);

                                                // print result

                                                printw("%c%c", '/', ch);

                                }

                                else

                                                printw("%c", ch); // print result

                                // perform refresh

                                refresh();

                                // If it is * and / then only switch bold off

                                if(prev == '*' && ch == '/')

                                                attroff(A_BOLD); // manipulate the highlights (Switch bold off)     

                                // assign ch to prev

                                prev = ch;

                }

                endwin(); // End curses mode

                // close the file

                fclose(fp);

                return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote