A small question about 'printf' | | |
Hi, group,
I am using a for loop and print such like:
for( i=0; i<10; i++)
printf("%d", i);
so the result look like '1234...', what I want is all the numbers are
in one position, mean the latter number overlap the former one, how to
do this? thank.
Vol. | | | | re: A small question about 'printf' volunteers@gmail.com wrote:[color=blue]
> Hi, group,
> I am using a for loop and print such like:
>
> for( i=0; i<10; i++)
> printf("%d", i);
>
> so the result look like '1234...', what I want is all the numbers are
> in one position, mean the latter number overlap the former one, how to
> do this? thank.[/color]
Are you looking for '\b'?
#include <stdio.h>
int main (void)
{
int i;
printf(" ");
for( i=0; i<12; i++)
printf("\b\b%2d", i);
putchar('\n');
return 0;
}
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. | | | | re: A small question about 'printf'
Michael Mair wrote:[color=blue]
> volunteers@gmail.com wrote:[color=green]
> > Hi, group,
> > I am using a for loop and print such like:
> >
> > for( i=0; i<10; i++)
> > printf("%d", i);
> >
> > so the result look like '1234...', what I want is all the numbers are
> > in one position, mean the latter number overlap the former one, how to
> > do this? thank.[/color]
>
> Are you looking for '\b'?
>
> #include <stdio.h>
>
> int main (void)
> {
> int i;
> printf(" ");
> for( i=0; i<12; i++)
> printf("\b\b%2d", i);
> putchar('\n');
> return 0;
> }
>[/color]
May well want
fflush(stdout);
in the loop. Or you often will only see the final number.
Example (system command specific to my system of course):
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i;
printf(" ");
for( i=0; i<12; i++) {
printf("\b\b%2d", i);
fflush(stdout);
system("sleep 1");
}
putchar('\n');
return 0;
}
Without the fflush, you see nothing until the last number.
The sleep is just there so it doesn't run so fast you see
nothing...
-David | | | | re: A small question about 'printf'
>I am using a for loop and print such like:[color=blue]
>
>for( i=0; i<10; i++)
> printf("%d", i);
>
>so the result look like '1234...', what I want is all the numbers are
>in one position, mean the latter number overlap the former one, how to
>do this? thank.[/color]
If you want to do a countdown, or a percent-complete indicator, or
something similar, \r can be used to go to the beginning of the
line and start writing over the previous output. It may or may not
work on printing terminals, and if it does "work", you may end up
with an unreadable mess. You may also want to output trailing
spaces, so if you're doing a countdown, it doesn't look like:
11
10
90
80
70
60
....
where the 0 is left on the screen from printing the 10.
for (i = 0; i < 10; i++) {
printf(" %d \r", i);
fflush(stdout);
/* do something time-consuming */
}
The leading space is there so the cursor doesn't cover up the number.
The trailing space is there to erase a digit from a prior number
in case the number of digits goes down (you might need more than
one, or perhaps use %5d to keep the number of characters printed
fixed). The \r returns you to the beginning of the line. The call
to fflush(stdout) is an attempt to get the output NOW rather than
all at once at the end in case this is being redirected to a file
or pipe or something.
I don't believe the standard guarantees that this will work 100%,
but it's pretty reliable in practice.
Gordon L. Burditt | | | | re: A small question about 'printf'
Michael Mair <Michael.Mair@invalid.invalid> wrote:[color=blue]
> volunteers@gmail.com wrote:[/color]
[color=blue][color=green]
>> for( i=0; i<10; i++)
>> printf("%d", i);
>>
>> so the result look like '1234...', what I want is all the numbers are
>> in one position, mean the latter number overlap the former one, how to
>> do this? thank.[/color]
>
> Are you looking for '\b'?[/color]
....[color=blue]
> for( i=0; i<12; i++)
> printf("\b\b%2d", i);[/color]
....
You might find '\r' useful, too.
--
Stan Tobias
mailx `echo siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g` | | | | re: A small question about 'printf' volunteers@gmail.com wrote On 09/26/05 16:32,:[color=blue]
> Hi, group,
> I am using a for loop and print such like:
>
> for( i=0; i<10; i++)
> printf("%d", i);
>
> so the result look like '1234...', what I want is all the numbers are
> in one position, mean the latter number overlap the former one, how to
> do this? thank.[/color]
This is Question 19.3 in the comp.lang.c Frequently
Asked Questions (FAQ) list http://www.eskimo.com/~scs/C-faq/top.html
-- Eric.Sosman@sun.com | | | | re: A small question about 'printf'
That would depend on your environment - your system.
One way would be to print a backspace char - or a c/r without the l/f.
--
---------------------------------------------------------------------
DataGet & PocketLog www.dataget.com
Data Collectors www.baxcode.com
--------------------------------------------------------------------
<volunteers@gmail.com> wrote in message
news:1127766727.267877.325710@g43g2000cwa.googlegr oups.com...[color=blue]
> Hi, group,
> I am using a for loop and print such like:
>
> for( i=0; i<10; i++)
> printf("%d", i);
>
> so the result look like '1234...', what I want is all the numbers are
> in one position, mean the latter number overlap the former one, how to
> do this? thank.
>
> Vol.
>[/color] | | | | re: A small question about 'printf'
Hi,
Thanks for all your replies. It solved the problem.
It seems '\b' or '\r' with make the cursor come back on the same line.
Is it possible that we can move the cursor to the above line? For
instance:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int i;
printf(" ");
for( i=0; i<12; i++) {
printf("\b\b%2d", i);
}
printf("%d\n", 15);
for( i=12; i<24; i++) {
printf(".........");
}
return 0;
}
Is it possible to let 'printf("........."); ' output the result on the
same line with 'printf("\b\b%2d", i);' and overlap it? Thanks.
Vol. | | | | re: A small question about 'printf' volunteers@gmail.com wrote:
# Hi,
# Thanks for all your replies. It solved the problem.
# It seems '\b' or '\r' with make the cursor come back on the same line.
# Is it possible that we can move the cursor to the above line? For
# instance:
Depends on the terminal or terminal emulator, text
window driver, etc. If your system has a curses library,
that will do what you want on a variety of terminals.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
We found a loophole; they can't keep us out anymore. | | | | re: A small question about 'printf'
On 26 Sep 2005 21:13:36 -0700, in comp.lang.c , volunteers@gmail.com
wrote:
[color=blue]
>Hi,
>Thanks for all your replies. It solved the problem.
>It seems '\b' or '\r' with make the cursor come back on the same line.
>Is it possible that we can move the cursor to the above line?[/color]
Not in standard C. Ask yourself how you'd do that on a teletype...
[color=blue]
> printf("\b\b%2d", i);[/color]
[color=blue]
> printf(".........");
>
>
>Is it possible to let 'printf("........."); ' output the result on the
>same line with 'printf("\b\b%2d", i);' and overlap it?[/color]
Yes, just don't output a \n, and rely on \r or \b working for your
system.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- | | | | re: A small question about 'printf'
On Tue, 27 Sep 2005 21:27:16 +0100, Mark McIntyre
<markmcintyre@spamcop.net> wrote:
[color=blue]
> On 26 Sep 2005 21:13:36 -0700, in comp.lang.c , volunteers@gmail.com
> wrote:
>[color=green]
> >Hi,
> >Thanks for all your replies. It solved the problem.
> >It seems '\b' or '\r' with make the cursor come back on the same line.
> >Is it possible that we can move the cursor to the above line?[/color]
>
> Not in standard C. Ask yourself how you'd do that on a teletype...[/color]
<OT="way">
The model 37 or 38, I forget which, had Half Line Reverse and Forward,
intended for superscripts and subscripts, but you could (usually?)
2*HLR several lines before the paper feed would start to misalign.
Probably even more with sprocket feed. And of course overprinting
either with 'up-line' or backspace or return is nondestructive.
- David.Thompson1 at worldnet.att.net |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|