printing the same spot in the terminal

J

Jacob Kjaergaard

Hi!

Im looking for a (relatively) easy way to print at the same spot on the
terminal. Like i.e wget does when showing the progressbar and the
percentage of the download.

if been looking in the source code to wget, but it seems very
comprehensive.

printf normally "scrolls" this is what I want to control.

i've tried manipulating the stdout stream with lseek() and fseek() to
rewind, but this doesn't seem to work. the fileposition ftell() returns -1 :(

anyone, any ideas?

/Jacob
 
M

Martin Dickopp

Jacob Kjaergaard said:
Im looking for a (relatively) easy way to print at the same spot on the
terminal. Like i.e wget does when showing the progressbar and the
percentage of the download.

If you're writing to a display device (like a terminal), you can use the
'\b' and '\r' control characters, which are intended to return to the
previous writing position and the beginning of the line, respectively.


#include <stdio.h>

int main (void)
{
int i;
for (i = 1; i <= 100; ++i)
{
/* do 1/100th of something */
printf ("\r%3d%% completed...", i);
fflush (stdout);
}
putchar ('\n');
return 0;
}


Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top