Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write to a specific position on the console?

Status
Not open for further replies.

sgursahaney

Programmer
Jun 11, 2001
57
US
Hello! I am looking for a way to print characters to a specific position on a console screen (STDOUT). I want to write the string "Processing...01" and replace the 01 with 02, 03, 04, 05, etc. every second. This requires that I be able to write to a specific position on the screen. printf() appends to the line. Does anybody know how I can do this?

Regards,
Suresh
 
You can include <conio.h> (unless you are in a linux/unix environment) and use the function gotoxy(int x, int y).

Otherwise you may use printf(&quot;\b&quot;) i.e., backspace in a way like this:

printf(&quot;%s&quot;, &quot;Processing... &quot;)
/* ... */
/* inside the loop */
printf(&quot;\b\b%02d&quot;, num); /* num = 1 or 2 or ... etc. /*
/* ... */

Hope this helps.

Bye.
Ankan. Please do correct me if I am wrong. :)
 
Hi

Or else you can use &quot;clrscr();&quot; inside the loop.This will clear your previous screen before printing &quot;Processing ..02&quot;

Regards

Vadiraj
 
#include<conio.h>
int main()
{
gotoxy(posX,posY);
.....
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
or #include <stdlib.h> and inside the loop you can use
Code:
#ifdef WIN32
system(&quot;cls&quot;); //win or dos
#else
system(&quot;clrscr&quot;); //unix-linux (i guess)
#endif
bluenote@uyuyuy.com
(excuse my english)
 
in TurboC/BorlandC also exists function clrscr() declared in conio.h John Fill
1c.bmp


ivfmd@mail.md
 
may be he/she's not using TurboC/BorlandC bluenote@uyuyuy.com
(excuse my english)
 
LOL Hey bluenote, I told about a possibility of TorboC/BorlandC. For me doesn't matter what does she use. John Fill
1c.bmp


ivfmd@mail.md
 
ok, don't be angry :~/ :eek:) LOL bluenote@uyuyuy.com
(excuse my english)
 
Hey,
Try this one.
outtextxy(x,y, str) if in graphics mode using graphics.h or try using \r which returns to the start of the line each time.
 
I still think ...

printf(&quot;%s&quot;, &quot;Processing... &quot;)
/* ... */
/* inside the loop */
printf(&quot;\b\b%02d&quot;, num); /* num = 1 or 2 or ... etc. /*
/* ... */

is the best way. Even if gotoxy is used:
gotoxy(x, y);
printf(&quot;%s&quot;, &quot;Processing... &quot;)
/* ... */
/* inside the loop */
printf(&quot;\b\b%02d&quot;, num); /* num = 1 or 2 or ... etc. /*
/* ... */

For one thing it works everywhere BorlandC or TurboC or Graphics or no Graphics or C or C++ or ... and so on.
And also it also is does not take much time (as compared to say clrscr()) (talking about performance)
Morever, it does not affect whatever else is displayed in the console.

:-9:cool::-D:cool::p:-V:-I|-0:cool:B-) ...LOL:-s::) Ankan.

Please do correct me if I am wrong. s-)
 
also you can try to use
#include<conio.h>
textbackground/textcolor(SOMECOLOR);
and after it
cprintf(....);//the same as printf but with color printing
use wherex/wherey to get cursor coordinates,
window(x1,y,x2,y2); //to set a rectangle zone in the console for printing in it. There when you use clrscr, will be cleared only screen on this window, and all background from thiis window will be changed in the last color used in testbackground for this window. Also see gettext/gettextinfo/movetext.
All theese function are borland specific. In VisualC for console is other API. John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for all the recommendations! I opted for the system(&quot;cls&quot;) approach because it seemed the easiest, and I am not using Borland C. My requirements were a little more complicated than I originally posted. In actuality, I have printed several lines to the screen and need to change values on all the lines. The backspace approach only allows me to change values on the last line I printed.

In a perfect world, I would like to jump to a specific place on the screen (like with the gotoxy() function) in order to reduce the &quot;flickering&quot; I get from having to rewrite the screen. Is there a comparable function in MS Visual C/C++?

Regards,
Suresh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top