2013-10-11 2 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

고정 막대를 사용하려면 작업을 완료하는 데 걸리는 시간을 알아야합니다. –