Joshua Bakker Joshua Bakker - 5 days ago 6
C Question

C ncurses getnstr length

I'm making a simple 'game' with ncurses in C, and for name input I use

getnstr
. I have the following code:

printw("What is your name? ");
char name[10];
int namelen = getnstr(name, 10);


Now, I want to allow max 10 characters, and I want to use the length of the name for the border columns. However,
namelen
is 0 for some reason (I thought
getnstr
returned the length).

How can I get the correct length of the name? So if I insert Josh as name I get 4.
sizeof(name)
returns 10 so it's no use.

Answer

Accordly to the documentation, getnstr doesn't return the length of the string but OK or ERR (http://pubs.opengroup.org/onlinepubs/007908799/xcurses/getnstr.html).

If you need the length of the string, use strlen (string.h).

PS : if you want to allow max 10 char, you need an array of 11 char (10 char + \0)

EDIT : Found on curses.h code :

#define ERR (-1)            /* Error return. */
#define OK  (0)         /* Success return. */
Comments