gpt4 book ai didi

c - 如何创建伪造的 "Loading..."序列?

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:50 26 4
gpt4 key购买 nike

所以我想让文本显示“正在加载”后跟 3 个延迟点,然后让它们重新开始,例如:

Loading.
Loading..
Loading...
Loading.

等等,但始终在同一行。

我查看了没有 dos.h 文件的延迟函数(因为我使用的是 gcc),但我不确定如何让这些点消失并从第一个开始重新开始.

最佳答案

您需要做两件事。

\r,或回车,将光标移回行首。

然后通常 stdout 在看到换行符之前不会显示任何内容。这称为缓冲,需要将其关闭。你可以用 setvbuf() 来做到这一点.

这是一个演示。

#include <stdio.h>
#include <unistd.h>

int
main()
{
/* Turn off stdout buffering */
setvbuf(stdout, NULL, _IONBF, 0);

for(int i = 0; i < 3; i++) {
/* Clear the current line by moving to the start,
overwriting it with spaces, and going back to the start.
*/
printf("\r \r");

printf("Loading");

/* Print ... over 3 seconds */
for(int i = 0; i < 3; i++) {
sleep(1);
printf(".");
}

sleep(1);
}

/* Finish it all up with a newline */
printf("\n");

return 0;
}

有更好的方法可以做到这一点 using the curses library , 但 \r 足以满足您的目的。

关于c - 如何创建伪造的 "Loading..."序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com