控制台应用程序中的进度条

时间:2013-10-11 04:34:55

标签: c++ visual-c++ encryption

我开始研究加密应用程序,但我似乎非常想到如何在显示进度条的情况下使用它。

任务很简单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循环中显示for循环的进度。

我知道我需要计算到目前为止已完成的工作量,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

您需要的是多线程。以下是进度条的一些示例源(来自: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;
}
相关问题