c++ - 函数为 std::String 返回空值
问题描述
用于从Flask refresh
Web 服务器获取输出结果,当执行该函数时,它会将所需的结果打印到屏幕上,但是当需要打印时,它不会打印输出值,您得到的只是一个空行
为什么会这样
size_t CurlWrite_CallbackFunc_StdString(void* contents, size_t size, size_t nmemb, std::string* s)
{
size_t newLength = size * nmemb;
try
{
s->append((char*)contents, newLength);
}
catch (std::bad_alloc& e)
{
//handle memory problem
return 0;
}
return newLength;
}
std::string refresh(std::string path) {
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
const char* path_ = path.c_str();
std::string s;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, path_);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "out=");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
/* 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: %s\n",
curl_easy_strerror(res));
}
/* always cleanup */
curl_easy_cleanup(curl);
}
std::cout << s << std::endl;
return s;
}
驱动程序代码
int main() {
std::string out = refresh(server_path+"/refresh");
std::cout << out << std::endl;
}
解决方案
推荐阅读
- sql-server - 如何从表中提取最后 2 个时期的数据
- javascript - Javascript数组减少聚合键
- r - 错误条错误地定位在 r 中的堆积条形图中
- sql-server - SSRS 比较两个表 - 多列与一列中的结果
- iis - 允许使用一个 NIC 在 Windows Server 2012 R2 上将多个 IP 用于出站流量
- html - Bootstrap - 4 个表并排且响应迅速
- producer-consumer - 信号量和线程同步
- inform7 - 当分数达到 100 时,我如何结束故事?通知 7
- bash - bash / 检查数字是否与列表中的数字匹配
- java - 如何在 recycleView 中创建 CardView?