2013-10-11 76 views
0

我開始研究加密應用程序,但我似乎非常想到如何讓它顯示進度條,因爲它工作。在控制檯應用程序運行期間的進度條

任務很簡單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循環,而它的工作原理。

我知道我需要計算到目前爲止做了多少,但我不知道如何去做。

+0

我可能會丟失一些東西很明顯,但爲什麼不乾脆明確和打印任何你想要在控制檯中看到和使用「我」或「X '多少次來展示吧? –

回答

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; 
} 
+0

這是否也適用於固定酒吧? –

+0

爲了有一個固定的酒吧,你將不得不知道完成任務需要多長時間。 –

相關問題