Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我如何创建一个假的;“正在加载…”;序列_C - Fatal编程技术网

C 我如何创建一个假的;“正在加载…”;序列

C 我如何创建一个假的;“正在加载…”;序列,c,C,所以我想让文本显示为“加载”,后跟3个延迟点,然后让它们重新开始,比如: Loading. Loading.. Loading... Loading. 等等,但总是在同一条线上 我查看了没有dos.h文件的delay函数(因为我使用的是gcc),但我不确定如何使点消失并从第一个点重新开始 \r或回车将光标移回行的开头 然后,在看到换行符之前,stdout通常不会显示任何内容。这称为缓冲,需要将其关闭。你可以用它来做这件事 这是一个演示 #include <stdio.h> #inc

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

Loading.
Loading..
Loading...
Loading.
等等,但总是在同一条线上


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

\r
或回车将光标移回行的开头

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

这是一个演示

#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;
}
#包括
#包括
int
main()
{
/*关闭标准输出缓冲*/
setvbuf(标准输出,空,0);
对于(int i=0;i<3;i++){
/*移动到起始位置以清除当前行,
用空格覆盖它,然后返回开始。
*/
printf(“\r\r”);
printf(“装载”);
/*打印…超过3秒*/
对于(int i=0;i<3;i++){
睡眠(1);
printf(“.”);
}
睡眠(1);
}
/*用新词结束这一切*/
printf(“\n”);
返回0;
}

有一些更奇特的方法可以做到这一点,但是
\r
对于您的目的来说已经足够了。

您需要两件事

\r
或回车将光标移回行的开头

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

这是一个演示

#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;
}
#包括
#包括
int
main()
{
/*关闭标准输出缓冲*/
setvbuf(标准输出,空,0);
对于(int i=0;i<3;i++){
/*移动到起始位置以清除当前行,
用空格覆盖它,然后返回开始。
*/
printf(“\r\r”);
printf(“装载”);
/*打印…超过3秒*/
对于(int i=0;i<3;i++){
睡眠(1);
printf(“.”);
}
睡眠(1);
}
/*用新词结束这一切*/
printf(“\n”);
返回0;
}

有一些更奇特的方法可以做到这一点,但是
\r
对于您的目的来说已经足够了。

您需要使用“\r”字符:

while (1) {
    int i;
    for (i=1; i<=3; i++) {
        printf("Loading%s\r", i==1 ? "." : i==2 ? "..":"...");
        fflush(stdout);
        sleep(1); /* or whatever */
    }
    printf("%20s\r", " ");
}
while(1){
int i;

对于(i=1;i您需要使用'\r'字符:

while (1) {
    int i;
    for (i=1; i<=3; i++) {
        printf("Loading%s\r", i==1 ? "." : i==2 ? "..":"...");
        fflush(stdout);
        sleep(1); /* or whatever */
    }
    printf("%20s\r", " ");
}
while(1){
int i;

对于(i=1;i字符'\b'将光标向后移动一个位置。以下序列将得到您想要的结果:

printf("Loading.");
while (not done yet) {
   delay
   printf(".");
   delay
   printf(".");
   delay
   printf("\b\b   \b\b"); // erase the last two dots, and move the cursor back again
}

字符“\b”将光标向后移动一个位置。以下序列将获得所需的内容:

printf("Loading.");
while (not done yet) {
   delay
   printf(".");
   delay
   printf(".");
   delay
   printf("\b\b   \b\b"); // erase the last two dots, and move the cursor back again
}
提示:
“\b\b\b\b\b\b”
提示:
“\b\b\b\b\b”