When creating a loading panel using printf and \ r in c, how to save the saved "moving" code

I create a loading panel .. and I almost do not have it, but when the words "=" add to myself, the end of my loading panel moves down the line. I want to stop it, instead

  • progress: |==== | 3%
  • progress: |============ |3%

I want

  • progress |== ............... | 3%

  • progress |========= .| 3%

It’s hard to see because of formatting, but I don’t want the second "|" to move with equal signs.

Here is my code; I think I need to tweak a little:

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

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("Usage: progress n\n");
        return 1;
    }

    int n = atoi(argv[1]);

    //printf("Enter a percentage: ");
    //scanf("%d", &n);
    //progress: |==================                                |  37%
    //int percentage = 37; 

    char display[70];
    char equals[69];
    char disp[20];

    for (int i = 0; i <=10* n; i++)
    {
        strcpy(display, "progress: |");
        strcat(equals, "=");
        strcpy(disp, "       |");

        char number[10];
        char space[10];

        sprintf(number, "  %d%%", i);
        sprintf(space, "%s", equals);

        strcat(display, space);
        strcat(display, disp);
        strcat(display, number);

        fprintf(stderr, "\r%s %d  \r%s",equals, i, display);
        usleep(1000000);
    }

    return 0;
} 
+4
source share
4 answers

An alternative way to implement something like the described one can use the substitution %.*sfor the operator fprintf, consider the following example:

const int n = 10;

// Initialize two arrays longer than the number of character to output
char s1[n+1] = "##########";
char s2[n+1] = "          ";

for (int i = 0; i <= n; i++)
{
    // Only output the first i characters of s1
    // And the first (n-i) characters of s2
    fprintf(stderr, "Progress: |%.*s%.*s| %02d\r", i, s1, n-i, s2, i);
    fflush(stderr);  //< Flush the output (just in case)
    usleep(1000000);
}

fprintf(stderr, "\n");
+2

, , . .

., char c if-else.

memset() , , , buf, NULL.



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

#define BUF_LEN     (1024)

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("Usage: progress n\n");
        return 1;
    }

    int n = atoi(argv[1]);
    char buf[BUF_LEN];
    const int iLimit = 10*n;

    int i, j;
    for (i = 0; i <= iLimit; i++)
    {
        memset(buf, 0, BUF_LEN);
        for ( j = 0; j < iLimit; j++ )
        {
            char c;
            if ( j <= i )
            {
                c = '=';
            }
            else
            {
                c = ' ';
            }
            buf[j] = c;
        }
        fprintf(stdout, "Progress: | %s | %%%03d", buf, (int)(i*100/iLimit));
        fflush(stdout);

        // Reset the cursor position
        fprintf(stdout, "\r");
        usleep(100000);
    }

    return 0;
}
+1

, :

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

int main(void)
{
    char spaces[80];
    char equals[80];

    /* Assume an 80-column screen */
    /* The 'progress: |' is 11 characters */
    /* There should be space for '| 100%' after it */
    /* So that 17 characters overhead. */
    /* We'll use 60 characters for the bar (not using 3) */

    for (int i = 0; i <= 100; i++)
    {
        /* Length of bar = (i * 60) / 100 */
        int barlen = (i * 60) / 100;
        int spclen = 60 - barlen;
        memset(equals, '=', barlen);
        equals[barlen] = '\0';
        memset(spaces, ' ', spclen);
        spaces[spclen] = '\0';
        fprintf(stderr, "\rprogress: |%s%s| %3d%%", equals, spaces, i);
        usleep(200000);
    }
    usleep(2000000);
    putchar('\n');

    return 0;
}

0 100 . = . , , memset(), . ( , printf() .) . ( \n ).

progress: |                                                            |   0%
progress: |                                                            |   1%
progress: |=                                                           |   2%
progress: |=                                                           |   3%
progress: |==                                                          |   4%
progress: |===                                                         |   5%
progress: |============================                                |  47%
progress: |============================                                |  48%
progress: |=============================                               |  49%
progress: |==============================                              |  50%
progress: |==============================                              |  51%
progress: |===============================                             |  52%
progress: |=========================================================   |  96%
progress: |==========================================================  |  97%
progress: |==========================================================  |  98%
progress: |=========================================================== |  99%
progress: |============================================================| 100%

:

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

int main(void)
{
    char spaces[80];

    /* Assume an 80-column screen */
    /* The 'progress: |' is 11 characters */
    /* There should be space for '| 100%' after it */
    /* So that 17 characters overhead. */
    /* We'll use 60 characters for the bar (not using 3) */

    memset(spaces, ' ', 60);
    spaces[60] = '\0';

    int oldbar = 0;
    for (int i = 0; i <= 100; i++)
    {
        /* Length of bar = (i * 60) / 100 */
        int newbar = (i * 60) / 100;
        if (oldbar != newbar)
            spaces[newbar-1] = '=';
        fprintf(stderr, "\rprogress: |%s| %3d%%", spaces, i);
        oldbar = newbar;
        usleep(200000);
    }
    usleep(2000000);
    putchar('\n');

    return 0;
}

. OTOH, , 200 , , .

+1
source

First, the code must determine how many columns will be accepted by the completed progress bar.

Then you need to determine the number of iterations per loop (usually the same as the maximum number of characters =)

then print the entire progress bar with all spaces.

then in a loop it prints the current number of characters =+ prints the current number of blank spaces.

0
source

Source: https://habr.com/ru/post/1628232/


All Articles