Joshua Bakker Joshua Bakker - 1 year ago 101
C Question

C ncurses getnstr length

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

. 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,
is 0 for some reason (I thought
returned the length).

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

Answer Source

Accordly to the documentation, getnstr doesn't return the length of the string but OK or ERR (

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. */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download