0

How do I read a line of text printed on a pad in ncurses? I am trying to use the ncurses function winchnstr. I am confused about how to use chtype* in the function. I understand that chtype is a long int, but when I use chtype in my code I get a segment fault. In the below example the long int y prints 20. I need to be able to read a line of text on the pad. Can someone show me how to do it?

long int p[20];
wmove(pad,prow,ccol);
long int y = winchnstr(pad, p,20)&A_CHARTEXT;

Edit:

Whenever I print the return from the function, I get the number of characters returned. I am confused. How can I turn this into a printable string?

fprintf(file,"%d",y);
codrelphi
  • 1,075
  • 1
  • 7
  • 13
Kevin Gardenhire
  • 626
  • 8
  • 22

2 Answers2

0

winchnstr adds a trailing null, and you already asked for it to return as many characters as would fit in the array. So that's addressing past the end of the array.

The manual page says

The four functions with n as the last argument, return a leading substring at most n characters long (exclusive of the trailing (chtype)0).

winchnstr returns chtype values, which have a character combined with video attributes. You cannot directly convert an array of chtype data to a string. But curses has a function winnstr which returns only a character string (no video attributes). Again, keep in mind that the trailing null is not included in the length which you specify for the function.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • That makes sense, I am still having trouble understanding how to get the string from the function. Can you check my edits and hopefully tell me the proper way to turn the output of winchstr into a string? – Kevin Gardenhire Mar 18 '18 at 15:06
0

Here is the code that works to extract a character for peoples use.

chtype p[150];
winchstr(pad, p);
char y = p[0] & A_CHARTEXT;

y will then hold the character under the cursor.

Kevin Gardenhire
  • 626
  • 8
  • 22
  • The screenshots [here](http://invisible-island.net/ncurses/ncurses-slang.html#compare_picsmap) were wider than 150 characters, so your suggested solution would not work. – Thomas Dickey Mar 19 '18 at 23:42