与C++进行交互
问题描述:
我用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 + +的交互?
答
要写入到控制台,使用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);
它会看到全线
_“你能解释我可以做与c + +的交互?”_使用'std :: cin'? – user0042
@ user0042! :D谢谢 – Julien