views:

55

answers:

4

Hello; I want to write a simple c++/c console app, to show my process 1% 2%. for now, i print it line by line like

"finsihed 1%" "finished 2%" and etc

how can i just update percentage x% without printing a new line?

and i want to open two console windows one show messages one show the process as above. how do i open another console window?

thank you for your help

+2  A: 

On most-all terminals, you can print the ASCII carriage return '\r' (value 13 decimal) to return the cursor to the left of the current line, allowing you to overwrite the previous value. Or, you can send backspaces ('\b', ASCII 8) to move a single character left. Neither will automatically remove content already displayed, but you can always overwrite anything you no longer want to see with some spaces. Alternatively, you can use control codes supported by your particular console (e.g. vt100, vt220...), which will probably have more advanced features such as "clear-to-end-of-line". Many libraries are available to detect the terminal type and use codes it supports, or synthesize advanced operations from many simpler ones where necessary: on Linux and UNIX, the ncurses library is a good choice.

C++ has no concept of console windows. Opening a second window depends a lot on the operating system you're using, and perhaps the graphics library, neither of which you specified. On any OS though, you can have your application write some messages into a file, then inspect that file from another window that you open yourself. On Linux/UNIX, the utility "less" is great for inspecting log files - as well as showing you the contents at the time you ran less, you can ask it to "follow" new data as it is written into the file.

Tony
i am using windows 7 64 bits
Nissan911
Hmmm... a little tricky as - to the best of my knowledge - Windows does come with a good utility to displaying growing file content from the console. You may want to write a program yourself to read from a message file, then to start that program from your mani program. If helpful, you can pass the second viewing program the filename as a command line argument ala `system("view c:\tmp\messages.txt")`.
Tony
A: 

You can update your progress message in several ways. You can print out backspace characters to move the cursor to the left and then write over the old output. If your console supports ANSI escape sequences, you can use an escape sequence to blank out the line and re-draw it.

The best technique to use will probably depend on the console you are using (different consoles support different things). What platform are you using, and what console?

bta
A: 

On windows you can move the cursor to any location on the screen, then start printing from there:

COORD c;
c.X = x; c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);

Of course, you need to include windows.h

PigBen
+1  A: 

It seems that you're talking about Windows console apps.

Each Windows process is associated with at most one console window.

It you want two console windows then you can start another process and pipe output to it, or communicate with it via sockets or Windows "mailslots" or whatever.

Alf P. Steinbach