I would like to create a loader animation in C. I can't get it right so I tried to keep it simple. I tried to do something like this: First it would appear:

[                                                                               ]
[|                                                                              ]
[||                                                                             ]

and so on...

I wanted to use the sleep function so it'll look like it's actually loading but not too slow, adding percents would be nice I guess, if it's a for loop with "i" as an integer counting its moves than it'll be printf("i%%"); as "%" wouldn't print "%" but "%%". Do you have any clue of how can I do this ?

Edit: That's what i've got so far, not working for a reason:

    for(i = 1 ; i <= 100 ; i++)
{
    Sleep(10);
    printf("\r");
    printf("[");
    for(j = 1 ; j <= i ; j++)
    {   
        printf("|");
    }
}
    printf("]");

but it would only print "[|" then "[||" then "[|||" and so on (it's printing way too much "|" too). I want it to print "[ ]" (100 spaces) and then "[| ]" so each time the first AND last brackets would be seen.

SECOND EDIT: accomplished this:

void changestr(char str[], int n)
{
    str[n] = '|';
}

void loader()
{
    int i, j, n = 0;
    char str[] = "                                                                                                    ";

    for(i = 0 ; i < 97 ; i++)
    {
        printf("\r[%-100s]", str);
        changestr(str, n);
        n++;
    }
}

and it's printing tons of lines and not overwriting the first.... What to do?

有帮助吗?

解决方案

Here's a windows-oriented idea:

#include <stdio.h>
#include <windows.h>

#define PROGRESS_BAR_SIZE 40

void print_n_chars(int n, int c) {
    while (n-- > 0) putchar(c);
}

void display_progress_bar(int p) {
    putchar('\r');
    putchar('[');
    print_n_chars(PROGRESS_BAR_SIZE * p / 100, '|');
    print_n_chars(PROGRESS_BAR_SIZE - PROGRESS_BAR_SIZE * p / 100, ' ');
    putchar(']');
}

int main(void) {
    int p;
    for (p = 0; p <= 100; ++p) {
        display_progress_bar(p);
        Sleep(100);
    }
    return 0;
}

Your code can work if you change it like this:

void loader() {
    int i;
    char str[] = "                                        ";
    for(i = 0 ; i <= 40 ; i++) {
        printf("\r[%s]", str);
        str[i] = '|';
        Sleep(100);
    }
}

其他提示

Just put your function(s) between demanding initialization commands of your program so you see real loading taking place such as compiling device functions, loading images, benching the machine, filling temporary files, ....

Changing position of cursor would depend on platform but you can use an empty string that fills entire screen exactly. Printing the updated string over and over should look like a progress.

First 80(size of a line ?) chars can be your loading indicator and the rest could be spaces or some "window"-looking nice things. (like a ╚ as a corner of window)

For example, zeroth char of string is "[" and 80th char is "]" and 1 to 79 are " " or "|" depending on the loading level. You can use a function to fill the char array / string and give output to screen.

Print it all in a single call:

printf("\r[%-100s]", stringWithBars);

So in the loop print the whole line as above, append a "bar" to the string, and continue the loop.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top