2017-07-16 129 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