doing initscr() in a loop causes error initialisat

2019-09-18 16:09发布

I'm trying to make a simple ncurses program to display a box with messages. Im following this link and it works well. The problem that I'm having is that if I put the whole code in a function and call it in a loop, the initialisation is having an error. As far as I know, if I called endwin() at the end of the function, there shouldn't be any problem calling the initscr() again. Am I missing a function to enable the initscr() to be called again?

This is the code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <curses.h>

int call()
{
    WINDOW *mainwin, *childwin;
    int ch;

    if ((mainwin = initscr()) == NULL) {
            fprintf(stderr, "Error initialising ncurses.\n");
            exit(EXIT_FAILURE);
    }

    noecho();
    curs_set(FALSE);

    keypad(mainwin, TRUE);

    mvaddstr(childwin, 1, 6, "Warning! Press q to exit");
    mvaddstr(childwin, 2, 15, "{ OK }");
    refresh();
    while( (ch = getch()) != 'q') {
            refresh();
    }

    delwin(mainwin);
    endwin();
    refresh();

    return EXIT_SUCCESS;
}

int main()
{
    int i;

    for (i=0;i<10;i++) {
            call();
    }

    return 0;
}

EDIT: I've put the header file. Change the code to be more simpler

标签: c ncurses curses
1条回答
神经病院院长
2楼-- · 2019-09-18 16:29

The sample program does not call initscr more than one. However, it does not show the #include lines either (necessary to compile), so perhaps it is not the program which was actually run.

Regarding a problem calling initscr (once or more than once), there are at least two possibilities:

  • you are using some other implementation of curses (e.g., a Unix one such as HPUX). As noted in X/Open, portable programs must not call initscr more than once.
  • the TERM variable is not set to a usable value. As usual, the manual page explains what the function does, and why it might fail.
查看更多
登录 后发表回答