What I want to do:
The cursor is initially blinking on the top left corner of the screen:
160 characters remaining
_
If you want to manage the complete screen, curses
is the way
to go. Otherwise, you can do a lot just using escape sequences;
see http://en.wikipedia.org/wiki/ANSI_escape_code, for example.
(Historically, such sequences varied from one terminal to the
next, and curses was originally a way of working around this.
Today, the ANSI escape codes are pretty universal for console
windows under a windowing system, being used by both Windows
console window and xterm.)
In addition to encapsulating the actual sequences, curses
supports character oriented input, with or without echo. This
is more difficult to do without curses, and is still very
unportable.
Win32
console doesn't support escape sequences. You can use Console API.
Tiny example that clears first 3 characters at (0, 0) from your console
#include <windows.h>
int main()
{
HANDLE hOutput = ::GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord = {0,0};
::SetConsoleCursorPosition(hOutput, coord);
char buff[] = " ";
::WriteConsoleA(hOutput, buff, 3, NULL, NULL);
return 0;
}
If you don't like Console API
and wish to use ncurses
analog, see there.
The first thing to understand is that C++ has no conception of a screen, as a standard part of the language. Standard output might be a file, a printer and cout doesn't know the difference.
The screen "device" itself, however, is typically a little smarter, and recognizes some commands. The most widely-implemented of these are '\r' - the carriage return and '\n' - the line feed. '\r' moves the cursor to the beginning of the line, and '\n' advances to the next line, but that's not fit into your needs as you've already tried.
It seems the only way forward here is to use curses (of which ncurses is only one implementation, though the standard one in Linux). It presents you with a virtual screen with various commands to update them. It then takes only the changed portions, and updates the terminal in an optimized way.
It's just an example of the typical C program using ncurses, could be worth to take a look:
#include <ncurses.h>
int main()
{
int ch;
initscr(); /* Start curses mode */
raw(); /* Line buffering disabled */
keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
noecho(); /* Don't echo() while we do getch */
printw("Type any character to see it in bold\n");
ch = getch(); /* If raw() hadn't been called
* we have to press enter before it
* gets to the program */
printw("The pressed key is ");
attron(A_BOLD);
printw("%c", ch);
attroff(A_BOLD);
refresh(); /* Print it on to the real screen */
getch(); /* Wait for user input */
endwin(); /* End curses mode */
return 0;
}
The printw() function writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).
You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.
One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the source code example above does).