控制台应用程序中操作期间的进度条

Progress bar during operation in console application

本文关键字:应用程序 操作 控制台      更新时间:2023-10-16

我开始开发一个加密应用程序,但我似乎大大考虑了如何让它在工作时显示进度条。

任务很简单 lSize 是被加密文件的总大小。

C++

中使用以下循环
//********** Open file **********
FILE * inFile = fopen (argv[1], "rb");
fseek(inFile , 0 , SEEK_END);
unsigned long lSize = ftell(inFile);
rewind(inFile);
unsigned char *text = (unsigned char*) malloc (sizeof(unsigned char)*lSize);
fread(text, 1, lSize, inFile);
fclose(inFile);
//*********** Encypt ************
unsigned char aesKey[32] = {
    /* Hiding this for now */
};
unsigned char *buf;
aes256_context ctx;
aes256_init(&ctx, aesKey);
for (unsigned long i = 0; i < lSize/16; i++) {
    buf = text + (i * 16);
    aes256_decrypt_ecb(&ctx, buf);
}
aes256_done(&ctx);
//******************************************************

我想知道如何在 for 循环工作时显示它的进度。

我知道我需要计算到目前为止做了多少,但我不知道该怎么做。

你需要的是

多线程。下面是进度条的一些示例源(来自:http://www.cplusplus.com/reference/future/future/(

#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // std::chrono::milliseconds
// a non-optimized way of checking for prime numbers:
bool is_prime (int x) {
   for (int i=2; i<x; ++i) if (x%i==0) return false;
   return true;
}
int main ()
{
   // call function asynchronously:
   std::future<bool> fut = std::async (is_prime,444444443); 
   // do something while waiting for function to set future:
   std::cout << "checking, please wait";
   std::chrono::milliseconds span (100);
   while (fut.wait_for(span)==std::future_status::timeout)
     std::cout << '.';
   bool x = fut.get();     // retrieve return value
   std::cout << "n444444443 " << (x?"is":"is not") << " prime.n";
   return 0;
}