, :
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char spaces[80];
char equals[80];
for (int i = 0; i <= 100; i++)
{
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];
memset(spaces, ' ', 60);
spaces[60] = '\0';
int oldbar = 0;
for (int i = 0; i <= 100; i++)
{
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 , , .
source
share