Reputation: 1329
#include <curses.h>
#include <fstream>
int main(){
initscr();
mvwprintw(stdscr, 1,1, "hello world");
wmove(stdscr, 1,1);
chtype* p = 0;
int n = 0;
n = winchstr(stdscr, p);
std::ofstream test("test.txt");
test << n << " " << (p == nullptr);
endwin();
}
In the above I print hello world
, move the cursor back to the start of the sentence, and attempt to store the contents of what is on screen in to p
using winchstr
. Except in the file test.txt
I see only the output -1 1
suggesting p
was unchanged and ERR
(i.e. -1) was returned, as the documentation describes:
int winchstr(WINDOW *win, chtype *ch);
Description: These routines read a chtype or cchar_t string from the window, starting at the current or specified position, and ending at the right margin, or after n elements, whichever is less.
Return Value: All functions return the number of elements read, or ERR on error.
Have I misunderstood the documentation? The contents of the line starting at hello world
should be stored in the location pointed to by p
after using winchstr
shouldn't it? Why is it returning error?
Upvotes: 0
Views: 334
Reputation: 54515
The location p
is a null pointer; ncurses will probably see that, and refuse to use it. The manual page says
No error conditions are defined. If the chstr parameter is null, no data is returned, and the return value is zero.
If you use winchstr
, with a non-null pointer, ncurses will (have to) assume you provide enough space for the result. Use winchnstr
to specify the size.
Upvotes: 1