显示进度 C

Display Progress C

所以我读了这样的数据

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;我不确定如何显示进度

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

当然,这只有在您的第一个 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.