-3

It doesn't print at coordinates y=10, x=20.

#include <stdio.h>
#include <curses.h>

int main()
{
    initscr();
    refresh();
    WINDOW *win;
    wmove(win, 10, 20);
    refresh();
    printf("hi\n"); 
    return 0;
}

When I execute it like this...

./a.out > op_file

This is what is op_file

[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi

Can someone explain...??

Sailesh010
  • 49
  • 6

4 Answers4

3

You must use initscr() function to initialize the screen and endwin() at the end to close the window...

If you move(), you must use refresh() or the cursor won't move physically.

Killian G.
  • 370
  • 2
  • 15
1

To move the cursor to a new position on a window, use the function int wmove(WINDOW *win, int y, int x)

wmove(win, y, x); where (x, y) are the coordinates of the new position in the window. If the window has nlines lines and ncolumns columns, then

  0 <= y < nlines
  0 <= x < ncolumns

Refresh. The actual cursor motion is not shown on the screen untill you do a wrefresh(win).

move(y, x) is equivalent to the wmove(stdscr, y, x).`

The move() and wmove() functions move the cursor associated with the current or specified window to (y, x) relative to the window's origin. This function does not move the terminal's cursor until the next refresh operation.

To move the logical cursor in the user-defined window my_window to the coordinates y = 5, x = 10, use :

#include <stdio.h>
#include <curses.h>

    int main(){
        refresh();//First refresh
        WINDOW *my_window;
        int a = wmove(my_window, 5, 10);
        refresh();////Second refresh
        printf("%d\n",a);
        printf("hi\n");
        return 0;
    }
Ediz Uslu
  • 119
  • 4
0

The output

[?1049h[1;24r(B[m[4l[?7h[H[2J-1
hi

shows the printable characters written. If you look at the complete text, e.g., in a text-editor, there'll be ASCII escape characters before the [ and ( characters since that's part of the escape sequence.

Your example doesn't show cursor movement (aside from the home position which you'd see as ^[[H near the end) because there's no reason for the curses library to actually move the cursor. If you had asked it to read a character, e.g., using getch, it would have to stop and decide where the cursor should be — and your wmove would do that — except that win is not initialized. The simplest thing to do is use stdscr (which is initialized by initscr).

The program quits curses calls without doing an endwin (which leaves the terminal in raw mode). The data does get written to the screen with the refresh call. The data written with printf happens to come out in the right order, but that's only coincidental since it does not use the same output buffering as ncurses.

Both of the other answers contain similar errors.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
-2

This works.

#include <stdio.h>
#include <curses.h>

int main()
{
    initscr();
    refresh();

    WINDOW *win;
    win = stdscr;

    wmove(win, 10, 10);

    refresh();
    printf("hi\n");

    return 0;
}

Thanks to @interjay.

Sailesh010
  • 49
  • 6