0

I am trying to test both printw and mvwprintw by printing a string at each new line. However they do not print anything.

This is the output on the terminal:

gcc -c -g *.c
gcc *.o -o main.exe -lncurses
DBOJANTC-M-KDD5:ncursestest user22$ main.exe
DBOJANTC-M-KDD5:ncursestest user22$ 

How do I make these functions print on the screen or window?

    int main() {

        char stuff[25] = "stuffstuff\n";
        int rows = 7;

        WINDOW* win;
        //int delwin(WINDOW *win);

        //printf("dddd\n");

        initscr();
        raw();
        noecho();
        printw("Try resizing your window(if possible) and then run this program again");
        win = newwin(rows, 80,  0, 0);


        for(int i = 0; i < rows; i++){

            mvwprintw(win, i, 0,"%s", stuff);

            wrefresh(win);
        }

    endwin();


    return 0;
}
A.boj
  • 1
  • 3

1 Answers1

2

printw and mvwprintw are actually printing. It is just that you are immediately refreshing the window or closing the window, thus you are not able to see the prints.

Try adding getch before closing or refreshing the window.

    for(int i = 0; i < rows; i++){

        mvwprintw(win, i, 0,"%s", stuff);
        getch();  //here
        wrefresh(win);
    }
getch();  //here
endwin();

Or even you can use sleep.

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • I can see the output now, but is there a way to make it visible without having to input during runtime? – A.boj Feb 14 '19 at 07:47
  • You can use `sleep`. – kiran Biradar Feb 14 '19 at 07:48
  • Using sleep, I cannot see the output without a huge delay. With a small delay nothing is printed – A.boj Feb 14 '19 at 08:17
  • kiran, @a.boj: refresh does not mean clear. It is the refresh call which causes the library to show the output. The call to sleep should go *after* the refresh. Getch works because getch automatically calls refresh before waiting for input. See the ncurses manpages. – rici Feb 14 '19 at 13:37
  • Even after I put sleep after refresh, nothing is printed. Also the ncurses manual does not mention anything about sleep. – A.boj Feb 14 '19 at 19:09
  • @A.boj, well, the ncurses manual does not mention `exit(2)` nor `sin(3)` also. `sleep(3)` makes the process to sleep for a number of seconds... so it's strange you don't get the output if you embed a `sleep(3)` at some part. A library is a collection of routines to be used in a program with some other routines also. Neither curses nor `sleep(3)` manual pages say the other cannot be used with it. – Luis Colorado Feb 15 '19 at 14:53