2014-11-26 5 views
0

내 프로그램이 오류없이 컴파일되고 모든 단계를 올바르게 실행하는 것처럼 보입니다. 그것은 PHP 호출을하고 데이터를 반환하기로되어 있습니다. tcpdump는 요청이 나가서 popen이 실행되고 있음을 보여 주지만 수신 측은 결코 업데이트되지 않습니다. 내가 찾을 수있는 유일한 불일치는 명령 변수에 데이터가 누락 된 것 같습니다.popen 호출시 데이터가 누락되었습니다

# 1

market max price is 0.00638671 at position 0 
php coin.php 155 0.006387 
0.00638672 

출력의 두 번째 줄은 그 아래의 것과 동일한 것으로 가정한다 I가

cout << command << endl; -> php coin.php 155 0.006387 

그 번호는 popen하는 보내고 명령이다 .trol.o 0.00638672 숫자 6과 숫자 2가 어떻게 든 잘립니다.

popen 명령에 올바른 데이터를 얻으려면 어떻게해야합니까?

코드 : 나는 biggestMark의 유형으로 긴 부동 소수점 형식 대신에 플로트를 사용하려고 할

void mngr(){ 
     //vector defs 
     vector<std::string> buydat; 
     vector<std::string> markdat; 
     vector<std::string> pricedat; 
     vector<std::string> purchaseid; 
     vector<double> doublePdat; 
     vector<double> doubleMdat; 
     doublePdat.reserve(pricedat.size()); 
     doubleMdat.reserve(markdat.size()); 
     char buybuff[BUFSIZ]; 
     char command[70]; 
     char sendbuy[12]; 
     buydat = getmyData(); 
     markdat = getmarketbuyData(); 
     //string match "Buy" and send results to new vector with pricedat.push_back() 
     for(int b = 2; b < buydat.size(); b+=7){ 
       if (buydat[b] == "Buy") { 
         pricedat.push_back(buydat[b+1]); 
       } 
     } 
     transform(pricedat.begin(), pricedat.end(), back_inserter(doublePdat), [](string const& val) {return stod(val);}); 
     transform(markdat.begin(), markdat.end(), back_inserter(doubleMdat), [](string const& val) {return stod(val);}); 
     auto biggestMy = std::max_element(std::begin(doublePdat), std::end(doublePdat)); 
     std::cout << "my max price is " << *biggestMy << " at position " << std::distance(std::begin(doublePdat), biggestMy) << std::endl; 
     auto biggestMark = std::max_element(std::begin(doubleMdat), std::end(doubleMdat)); 
     std::cout << "market max price is " << *biggestMark << " at position " << std::distance(std::begin(doubleMdat), biggestMark) << std::endl; 
     if (biggestMy > biggestMark){ 
       cout << "Biggest is Mine!" << endl; 
     } 
     else if (biggestMy < biggestMark){ 
       //cout << "Biggest is market!"; 
       *biggestMark += 0.00000001; 
       sprintf(sendbuy,"%f",*biggestMark); 
       sprintf(command, "php coin.php 155 %s",sendbuy); 
       FILE *markbuy = popen(command, "r"); 
       if (markbuy == NULL) perror ("Error opening file"); 
       while(fgets(buybuff, sizeof(buybuff), markbuy) != NULL){ 
         size_t h = strlen(buybuff); 
         //clean '\0' from fgets 
         if (h && buybuff[h - 1] == '\n') buybuff[h - 1] = '\0'; 
         if (buybuff[0] != '\0') purchaseid.push_back(buybuff); 
       } 

       cout << command << endl; 
       cout << *biggestMark << endl; 

     } 
} 

답변

1

복식에서 반복자로 평가되어야한다. sprintf(sendbuy,"%f",*biggestMark);sprintf(sendbuy,"%lf",*biggestMark);으로 변경하려고합니다. 희망이 도움이 될 것입니다.

+0

그건 개선했습니다. 점진적으로 떨어지는 대신에, 그것은 단지 마지막 2를 깔끔하게 먹었습니다. 그러나 마지막 2 자리가 여전히 누락되었습니다. :(내 최대 가격은 위치 2 시장 최대 가격 0.00521413 것은 위치에 0.00642538 155 0.006425 0.00642539 – nerd007

+0

부동 소수점 표현의 제한이 관련이 없습니다 /home/coinz/coin.php 0 는/usr/빈/php에있다 IEEE 754 표준은 "* largestMark + = 1E-8'을 수행 할 때 특히 부동 소수점 숫자를 사용하는 것이 어렵습니다. 코드에서 그러한 진술을 피하십시오. – Aleksei

+0

* largestMark = * largestMark + 0.00000001 ; – nerd007

관련 문제