显示进度C.

问题描述 投票:0回答:1

所以我读了这样的数据

if (contentLengthType == CONTENTLEN_EXIST) {
        printf("Content-Length = %lu\n", contentLength);
    }
{

    int fd = open(dst, O_WRONLY | O_CREAT, 0777);
    int total_read = 0;
    if (fd < 0) {
        return fd;
    }

    while (1) {
        int read = readData(req, buf, sizeof(buf));
        if (read < 0) {
            return read;
        }
        if (read == 0)
            break;
        ret = write(fd, buf, read);
        if (ret < 0 || ret != read) {
            if (ret < 0)
                return ret;
            return -1;
        }
        total_read += read;
    }

但是我喜欢它使用我的进度条显示基于内容长度的写入进度,如此处所示int32_t progress_steps = 10;我不知道如何显示进度

c progress-bar progressdialog
1个回答
0
投票

您只需计算到目前为止读取的总长度的哪一部分,并缩放到显示进度指示的所需值。

当然,这只适用于您在第一个if中检查的内容长度。

基本上它归结为一些基本的数学练习,你必须在每一轮循环中执行。

#define PROGRESS_STEPS 10.0

// calculate part of total task in range 0.0..10.0
float progress = (float)total_read / content_length * PROGRESS_STEPS;

// Apply some rounding according to personal preferences

// Print current progress indicator
printf("%d/%d\n", (int) progress, (int) PROGRESS_STEPS);
// Depending on terminal you might add code to use same line for each output.
© www.soinside.com 2019 - 2024. All rights reserved.