如何在C++中使用 cURL 让用户输入 URL 的搜索词
How to have user input for the search term of a URL using cURL in C++?
我正在尝试更改URL的搜索词,而不是在C++的cURL中预定义一个搜索词。谁能帮我解决这个问题?
我在下面提供了一些关于我尝试的方法的代码,但无济于事:
#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
using namespace::std;
int main(void)
{
CURL *curl;
CURLcode res;
string searchTerm;
cout<<"Enter ticker name: "<<endl;
cin>>searchTerm;
cout<<"https://ichart.finance.yahoo.com/table.csv?s="<<searchTerm<<endl;
string whatURL = "https://ichart.finance.yahoo.com/table.csv?s="+searchTerm;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, whatURL); //the error I get is in this line
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %sn",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
//int argc, const char * argv[]
return 0;
}
我得到的实际错误是:无法通过可变参数函数传递非平凡类型"字符串"(又名"basic_string,分配器>")的对象;调用将在运行时中止
注意:我在网上找到了一种使用 PHP 工作的方法,但是我不知道如何将 PHP 实现到我的C++文件中(作为一种可能的解决方法)。
提前感谢,
你必须记住,libcurl 是一个 C API,只有 C 函数。幸运的是,您可以直接从C++使用它们,但不能使用 C 中不可用的任何内容(例如对象实例)调用它们。
这里你需要传递一个字符串指针,你可以使用 std::string::c_str
函数来获取它:
curl_easy_setopt(curl, CURLOPT_URL, whatURL.c_str());
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 用户定义函数中的指针和输入
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 使用 for 循环在数组中输入用户