2017-07-16 128 views
-3

我用C++(cli)寫了一個小程序,它允許在網站上啓動一個請求。與C++進行交互

#include <iostream> 
#include <string> 
#include <curl/curl.h> 


static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) 
{ 
    ((std::string*)userp)->append((char*)contents, size * nmemb); 
    return size * nmemb; 
} 

int main(void) 
{ 
    CURL *curl; 
    CURLcode res; 
    std::string readBuffer; 

    curl = curl_easy_init(); 
    if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/?id=1&hash=123456"); 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); 
    res = curl_easy_perform(curl); 
    curl_easy_cleanup(curl); 

    std::cout << readBuffer << std::endl; 
    } 
    return 0; 
} 

此代碼工作得很好,符合我要求

我需要創建一個交互式版本修改URL的變量的任務。該程序的輸出 例子:

./my_prog 
What content do you want to add in the variable id? 
1 
What content do you want to add in the hash variable? 
123456879 

而且這些問題後,將推出捲曲與查詢的問題的結果。
你能解釋我可以做與c + +的交互?

+1

_「你能解釋我可以做與c + +的交互?」_使用'std :: cin'? – user0042

+0

@ user0042! :D謝謝 – Julien

回答

1

要寫入到控制檯,使用std::cout << "something"以獲取輸入,使用std::cin >> x;

在你的榜樣它是這樣的:

std::string id, hash; 
std::cout << "What content do you want to add in the variable id?\n"; 
std::cin >> id; 
std::cout << "What content do you want to add in the hash variable?\n"; 
std::cin >> hash; 

插入新的數據到URL,替換這一行:

curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/?id=" + id + "&hash=" + hash); 

std::cin將讀取輸入,直到第一個空格或底線字符,如果由於某種原因,你需要輸入SEV你可以使用getline (std::cin, hash);它會看到全線

+0

OP的問題可能是他們需要首先提取哪些參數應該從該特定URL填充。我相信他們已經意識到如何通過'std :: cin'進行通信。 – user0042

+0

我會更新答案,如果是這樣的話。 「與C++的交互」很難理解,也許問題的擁有者會詳細說明 –

+0

除非問題不清楚,否則你應該不會寫出答案。這就是評論的目的。沒有那麼難得到失蹤的20名代表,讓你在任何地方發表評論。 – user0042