关于'printf'的一个小问题 [英] A small question about 'printf'

查看:97
本文介绍了关于'printf'的一个小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小组,

我正在使用for循环和打印如下:


for(i = 0; i< 10; i ++)

printf("%d",i);


所以结果看起来像''1234 ......',我想要的就是所有数字是

在一个位置,意味着后一个数字与前一个数字重叠,如何
这样做?感谢




解决方案
vo ******** @ gmail.com 写道:

小组,
我是使用for循环和打印如下:

for(i = 0; i< 10; i ++)
printf("%d",i);




你在寻找''\ b''吗?


#include< stdio.h>


int main(无效)

{

int i;

printf("" ;);

for(i = 0; i< 12; i ++)

printf(" \b\b%2d",i);

putchar(''\ n'');

返回0;

}


干杯

Michael

-

电子邮箱:我的是/ at / gmx / dot / de地址。




Michael Mair写道:

vo ******** @ gmail.com 写道:

小组,
我正在使用for循环和打印如下:

for(i = 0; i< 10; i ++)
printf("%d",i);

所以结果看起来像''1234 .. 。'',我想要的是所有数字都在一个位置,意味着后一个数字与前一个数字重叠,怎么做呢?谢谢。



你在找''\ b''吗?

#include< stdio.h>

int main(void)
{
int i;
printf("");
for(i = 0; i< 12; i ++)
printf (" \\\\\\\\\\\\\\\;
putchar(''\ n'');
返回0;
}



可能需要


fflush(stdout);

循环中的
。或者你经常只会看到最终的数字。

例子(系统命令特定于我的系统当然):


#include< stdio.h>

#include< stdlib.h>

int main(无效)

{

int i; <登记/>
的printf(QUOT;");

为(I = 0; I< 12;我++){

的printf(QUOT; \ b\b%2d",i);

fflush(stdout);

system(sleep 1);


}

putchar(''\ n'');

返回0;

}


如果没有fflush,你会看到最后一个号码之前什么都没有。

睡觉就在那里所以它运行得不那么快你看到

什么都没有...


-David


>我正在使用for循环并打印如下:块引用类= post_quotes>
为(I = 0; I< 10;我++)
的printf(QUOT;%d",I);

所以结果看就像''1234 ......',我想要的就是全部数字在一个位置,意味着后一个数字与前一个重叠,如何做到这一点?谢谢。




如果你想做一个倒计时,或百分比完成指标,或

类似的东西,\ r可以是曾经去过

行的开头并开始写上一个输出。它可能会或可能不会在打印终端上工作,如果它正常工作,你可能会因为一个难以理解的混乱而最终获得
。你可能还想输出尾随的

空格,所以如果你正在进行倒计时,它看起来不像:


11

10

90

80

70

60

....


其中0在屏幕上留下来打印10.


for(i = 0; i< ; 10;我++){

的printf(QUOT;%d \r",I);

fflush(stdout中);

/ *做一些耗时的事情* /

}


领先的空间就在那里,因此光标并没有掩盖这个数字。

尾随空格用于擦除前一个数字的数字

,以防数字位数下降(您可能需要超过

一个,或者也许使用%5d来保持打印的字符数

固定)。 \r会将您返回到行的开头。对fflush(stdout)的调用

是尝试在结束时立即获取输出而不是

,以防将其重定向到文件

或管道什么的。


我不相信标准保证这将100%工作,

但它'在实践中非常可靠。


Gordon L. Burditt


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.

解决方案

vo********@gmail.com wrote:

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.



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.



Michael Mair wrote:

vo********@gmail.com wrote:

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.



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;
}



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


>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.



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


这篇关于关于'printf'的一个小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆