将服务器响应(std :: string)转换为png文件 [英] Convert server response (std::string) into a png file
问题描述
我正在使用boost :: asio从服务器获得响应。结果存储在std :: string中。
I'm getting a response from a server using boost::asio. The result is stored in a std::string.
我想将此std :: string转换为.png图像并将其写入文件。
I want to convert this std::string into a .png image and write it to file.
我遇到了这个问题。这是我的代码:
I'm having awful trouble with this. Here is my code:
CURL *curl;
CURLcode res;
std::string errorBuffer[CURL_ERROR_SIZE];
std::string url="http://www.google.ie/images/srpr/logo3w.png";
std::string response="";
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res=curl_easy_perform(curl);
curl_easy_cleanup(curl);
现在存储在响应中的响应
write_to_binary_file(response.data());
,其中write_to_binary_file为:
, where write_to_binary_file is:
void write_to_binary_file(std::string p_Data){
//std::fstream binary_file("./img.png",std::ios::out|std::ios::binary|std::ios::app);
//binary_file.write(reinterpret_cast<const char*>(&p_Data),sizeof(std::string));
//binary_file.close();
std::ofstream file("img.png", std::ios::binary);
file.write(p_Data,sizeof(p_Data));
file.close();
}
现在如果我对我的C ++程序写的文件进行八进制转储,它与我直接从URL下载文件时得到的八进制转储完全不同。
Now if I do an octal dump on the file written by my C++ program, it's totally different to the octal dump I get when I download the file from the URL directly.
更新了write_to_binary_file
Updated write_to_binary_file
int write_to_binary_file(const char* p_Data){
//std::fstream binary_file("./img.png",std::ios::out|std::ios::binary|std::ios::app);
//binary_file.write(reinterpret_cast<const char*>(&p_Data),sizeof(std::string));
//binary_file.write(c_str(),sizeof(ring));
//binary_file.close();
/*std::ofstream file("img.png", std::ios::binary);
file.write(p_Data,len);
file.close();*/
FILE *fp;
size_t count;
const char *str = p_Data;
fp = fopen("img.png", "w");
if(fp == NULL) {
perror("failed to open img.png");
return EXIT_FAILURE;
}
count = fwrite(str, 1, strlen(str), fp);
printf("Wrote %zu bytes. fclose(fp) %s.\n", count, fclose(fp) == 0 ? "succeeded" : "failed");
return EXIT_SUCCESS;
}
使用调用int x = write_to_binary_file(response.c_str) ());
仍然不适用于我;(
推荐答案
binary_file.write(reinterpret_cast<const char*>(&p_Data),sizeof(std::string));
对,这就是为什么 reinterpret_cast
在许多情况下很差:提升 guesscoding 。
Right, this is why reinterpret_cast
is poor in many scenarios: it promotes guesscoding.
这是你抛弃你的参考资料而只是假设你需要将指针传递给 std :: string
某处,并确定编译器的错误消息是错误的。所以,你用 reinterpret_cast
来关闭它 ,现在你想知道为什么没有什么工作正常。
This is when you've thrown away your reference materials and just assumed that you need to pass a pointer to an std::string
somewhere, and decided that the compiler's error messages were wrong. So, you used reinterpret_cast
to "shut it up", and now you're wondering why nothing's working properly.
据推测有人告诉你使用 std :: string
而不是char数组,你刚刚交换了类型名称,而不是对实际的差异是。
Presumably somebody has told you to "use std::string
" instead of char arrays, and you've just swapped the type names rather than doing any research into what the actual difference is.
std :: string
是一个具有各种内部成员的对象,它通常间接地存储它的实际字符串数据(动态地,或者你可能不准确和误导地称为在堆上);事实上,它完全是从你那里抽象出来的,只是它如何存储它的数据。无论哪种方式,它不仅仅是一个 char
数组。
std::string
is an object with various internal members, and which usually stores its actual string data indirectly (dynamically, or what you may inaccurately and misleadingly refer to as "on the heap"); in fact, it's completely abstracted away from you as to just how it stores its data. Either way, it's not just a char
array.
使用它提供的API来获取指向a的指针 char
数组,包含 std :: string :: data()
或可能 std: :string :: c_str()
...和停止猜测。
Use the API that it provides to obtain a pointer to a char
array, with either std::string::data()
or possibly std::string::c_str()
... and stop guesscoding.
另外,我怀疑这个编译:
Also, I doubt that this compiles:
std::string errorBuffer[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
你可能想构建一个 std :: string
具有一定长度:
And you probably wanted to construct a std::string
with a certain length:
std::string errorBuffer(CURL_ERROR_SIZE);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errorBuffer[0]);
// ^ (requires C++11 to be completely safe)
字符串是对象,而不是原始数组!
这篇关于将服务器响应(std :: string)转换为png文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!