我知道基于标题,我的问题没有多大意义,但希望这个例子可以。
例:
printf("hello world\n\n\n");
printf("hello again");
printf("bye now");
printf("this should print on line 2");
printf("this should print on line 3");
printf("this should print on line 4");
expected output
1. hello world
2. this should print on line 2
3. this should print on line 3
4. this should print on line 4
5. hello again
6. bye now我的目标是先打印出第1行、第5行和第6行,然后打印出第2-4行(包括在内),中间是1秒。我知道你可以在两秒钟内用睡眠打印出线条。但是我不知道如何打印出第5行和第6行之后的第2-4行。
发布于 2022-02-02 18:37:34
这应该可以做到:
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("hello world\n");
sleep(1);
printf("\n\n\n");
printf("hello again\n");
sleep(1);
printf("bye now\n");
sleep(1);
printf("\033[5A"); /* move up 5 lines */
printf("but wait\n");
sleep(1);
printf("there's more\n");
}当然,魔法就在这条线上
printf("\033[5A");序列\033打印ASCII字符(八进制33,十六进制1b)。四个字符的序列
Esc [ 5 A将光标向上移动5行。
这假设您有一个实现ANSI标准游标运动序列的终端(或终端仿真器),但大多数是这样的。(很久以前,有很多不同的终端使用不同的游标运动约定,建议使用termcap和/或ncurses包将代码与这些细节隔离开来。)
这些ANSI序列不是C语言的一部分(不是由C语言指定的),但正如我所说的那样,它们往往能工作。最有用的可能是ESC [ rr ; cc H,它将移动到任意行rr和列cc。还有更多的这些序列;我刚刚找到的一个不错的参考是https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797。
https://stackoverflow.com/questions/70960863
复制相似问题