[提问帖][已解决]Ubuntu中学习Linux C,关于刷新printf输出问题。
-
- 帖子: 15
- 注册时间: 2008-05-17 23:33
[提问帖][已解决]Ubuntu中学习Linux C,关于刷新printf输出问题。
不能上传图片啊……昏……
直接描述吧:
int i = 0;
while(1)
{
printf("now i = %d\r", i);
fflush(stdout);
i++;
sleep(1);
}
我想在输出中不仅仅是显示最后一行,怎么改写这个代码?我想要有好几行同时在更新输出。
比如同时输出多个printf的更新:
now i = 0
now i = 0
now i = 0
...
这种类型,而不是文中代码里那样,只有一行输出是更新的。
已经解决,答案在5楼。呵呵
直接描述吧:
int i = 0;
while(1)
{
printf("now i = %d\r", i);
fflush(stdout);
i++;
sleep(1);
}
我想在输出中不仅仅是显示最后一行,怎么改写这个代码?我想要有好几行同时在更新输出。
比如同时输出多个printf的更新:
now i = 0
now i = 0
now i = 0
...
这种类型,而不是文中代码里那样,只有一行输出是更新的。
已经解决,答案在5楼。呵呵
- 附件
-
- 这个是图片
- 未命名.jpg (6.56 KiB) 查看 3025 次
上次由 rum24 在 2011-08-03 16:05,总共编辑 1 次。
- cuihao
- 帖子: 4793
- 注册时间: 2008-07-24 11:33
- 来自: 郑州
- 联系:
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
不知所云。你的意思是:
now i = 0
now i = 0
now i = 0
==========flush=============
now i = 1
now i = 1
now i = 1
==========flush=============
...
吗?
now i = 0
now i = 0
now i = 0
==========flush=============
now i = 1
now i = 1
now i = 1
==========flush=============
...
吗?
求人不如求它仨: 天蓝的Wiki 屎黄的Wiki 蓝红黄蓝绿红
Site: CUIHAO.TK Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
Site: CUIHAO.TK Twitter: @cuihaoleo
Machine: Athlon64 X2 5200+ / 2x2GB DDR2-800 / GeForce GTS 450
AD: ~まだ見ぬ誰かの笑顔のために~
-
- 帖子: 15
- 注册时间: 2008-05-17 23:33
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
我的意思是:想要实时的更新printf的输出,用来做游戏用的,不停的刷新输出来更新游戏画面。
效果应该是要这样的:
now i is 0
now i is 0
now i is 0
然后在sleep(1)之后,上面的三行,更新,而不是换行重新输出。更新成
now i is 1
now i is 1
now i is 1
这种情况的。
就是在原来的三行上继续更新,而不是换行,然后再更新。
效果应该是要这样的:
now i is 0
now i is 0
now i is 0
然后在sleep(1)之后,上面的三行,更新,而不是换行重新输出。更新成
now i is 1
now i is 1
now i is 1
这种情况的。
就是在原来的三行上继续更新,而不是换行,然后再更新。
- fanhe
- 帖子: 2357
- 注册时间: 2007-03-24 23:45
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
这个不行的吧
你换行了就没办法把光标放到换行前的位置了的
好像只能一直刷新同一行的
你把需要同时显示的用空格分开凑到一行不就行了
要不然就只能把整个标准输出清空再重新打印, 这样不会快
你换行了就没办法把光标放到换行前的位置了的
好像只能一直刷新同一行的
你把需要同时显示的用空格分开凑到一行不就行了
要不然就只能把整个标准输出清空再重新打印, 这样不会快
-
- 帖子: 15
- 注册时间: 2008-05-17 23:33
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
找到答案了,既然我有这个问题,估计别人也有,我顺便把我认为可以实现这个功能的答案贴上来吧。
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6 while(1)
7 {
8 printf("%d\n", i);
9 printf("%d\n", i);
10 printf("%d\n", i);
11 printf("\033[3A");
12 printf("\033[K");
13 i++;
14 sleep(1);
15 }
16 return 0;
17 }
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6 while(1)
7 {
8 printf("%d\n", i);
9 printf("%d\n", i);
10 printf("%d\n", i);
11 printf("\033[3A");
12 printf("\033[K");
13 i++;
14 sleep(1);
15 }
16 return 0;
17 }
- cjxgm
- 帖子: 1952
- 注册时间: 2010-04-23 20:40
- 系统: Arch Linux
- 来自: 浙江·杭州
- 联系:
Re: [提问帖][已解决]Ubuntu中学习Linux C,关于刷新printf输出问题。
就是像 mplayer 那样啊
其实我一直是用 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); 来实现的
其实我一直是用 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); 来实现的

- fanhe
- 帖子: 2357
- 注册时间: 2007-03-24 23:45
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
那几个转义字符啥意思?rum24 写了:找到答案了,既然我有这个问题,估计别人也有,我顺便把我认为可以实现这个功能的答案贴上来吧。
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6 while(1)
7 {
8 printf("%d\n", i);
9 printf("%d\n", i);
10 printf("%d\n", i);
11 printf("\033[3A");
12 printf("\033[K");
13 i++;
14 sleep(1);
15 }
16 return 0;
17 }
- cjxgm
- 帖子: 1952
- 注册时间: 2010-04-23 20:40
- 系统: Arch Linux
- 来自: 浙江·杭州
- 联系:
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
\e[ 或者写作 \033[ 是 CSI,用来操作屏幕的。fanhe 写了:那几个转义字符啥意思?rum24 写了:找到答案了,既然我有这个问题,估计别人也有,我顺便把我认为可以实现这个功能的答案贴上来吧。
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6 while(1)
7 {
8 printf("%d\n", i);
9 printf("%d\n", i);
10 printf("%d\n", i);
11 printf("\033[3A");
12 printf("\033[K");
13 i++;
14 sleep(1);
15 }
16 return 0;
17 }
我昨天刚学习了一些
\e[K 表示从光标当前位置起删除到 EOL (行尾)
\e[NX 表示将光标往X方向移动N,X = A(上) / B(下) / C(左) / D(右),\e[3A 就是把光标向上移动3行
-
- 帖子: 15
- 注册时间: 2008-05-17 23:33
Re: [提问帖]Ubuntu中学习Linux C,关于刷新printf输出问题。
是滴,是这么回事,实在找不到方法,只能用这种看来很灰主流的方式,这样,至少屏幕开始刷新了,呵呵。cjxgm 写了:\e[ 或者写作 \033[ 是 CSI,用来操作屏幕的。fanhe 写了:那几个转义字符啥意思?rum24 写了:找到答案了,既然我有这个问题,估计别人也有,我顺便把我认为可以实现这个功能的答案贴上来吧。
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i = 0;
6 while(1)
7 {
8 printf("%d\n", i);
9 printf("%d\n", i);
10 printf("%d\n", i);
11 printf("\033[3A");
12 printf("\033[K");
13 i++;
14 sleep(1);
15 }
16 return 0;
17 }
我昨天刚学习了一些
\e[K 表示从光标当前位置起删除到 EOL (行尾)
\e[NX 表示将光标往X方向移动N,X = A(上) / B(下) / C(左) / D(右),\e[3A 就是把光标向上移动3行
- cjxgm
- 帖子: 1952
- 注册时间: 2010-04-23 20:40
- 系统: Arch Linux
- 来自: 浙江·杭州
- 联系:
Re: [提问帖][已解决]Ubuntu中学习Linux C,关于刷新printf输出问题。
一点也不灰主流,一般都是这么搞的
更牛一点的用 curses

更牛一点的用 curses
-
- 帖子: 15
- 注册时间: 2008-05-17 23:33
Re: [提问帖][已解决]Ubuntu中学习Linux C,关于刷新printf输出问题。
顶楼上的,看来这个方法更好,呵呵。顺便附上百度来的资料:
http://baike.baidu.com/view/2205825.htm
http://baike.baidu.com/view/2205825.htm