简体   繁体   中英

probleme with printw, ncurses

I need to use printw(); but it doesn't work, it just segfault. I have used initscr and endwin but it doesn't change anything.

void    aff_battel(char **str)
{
  int   i;
  int   j;
  char  *str2;

  if ((str2 = malloc(sizeof(*str) * 23)) == NULL)
    return ;
  str2 = "---------------------\n\0";
  j = 0;
  i = 0;
  while (str[i] != NULL)
    {
      initscr();
      printw("---------------------\n\0"); /* it doesn't work */
      printw("%s", str2); /* here nether */
      endwin();
      i++;
    }
}

know i have put initsrc(); at the start and endwin a the end of the loop but it still print me nothing

void    aff_battel(char **str)
{
  int   i;
  int   j;

  j = 0;
  i = 0;
  while (str[i] != NULL)
    {
      printw("---------------------\n");
      i++;
    }
  endwin();
}

As Joachim Pileborg said, in the comments, initscr() should be at the start of your program, and endwin() at the end.

initscr() sets up an internal screen character map, to which printw() will write.

To copy the screen map to the actual screen you need to call refresh() . Without it, you will see nothing.

endwin() clears the screen and resets the terminal to normal.

Here is an example using your simplified function to demonstrate the ncurses functions. I have used getch() to make the program wait for a key press and then exit.

Note: In this example it would still display the text without the refresh() as getch() does a refresh too.

#include <ncurses.h>

void    aff_battel(char **str)
{
  int   i;
  int   j;

  j = 0;
  i = 0;
  while (str[i] != NULL)
  {
      printw("---------------------\n");
      i++;
  }
  refresh();
}

void main(int argc, char **argv)
{
    initscr();
    aff_battel(argv);
    getch();
    endwin();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM