使用 C++ 将二进制文件 (jpg) 读取为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17584784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a binary file (jpg) to a string using c++
提问by zoujyjs
I need to read a jpg file to a string. I want to upload this file to our server, I just find out that the API requires a string as the data of this pic. I followed the suggestions in a former question I've asked Upload pics to a server using c++.
我需要将 jpg 文件读取为字符串。我想把这个文件上传到我们的服务器,我只是发现API需要一个字符串作为这张图片的数据。我按照前一个问题中的建议使用 c++ 将图片上传到服务器。
int main() {
ifstream fin("cloud.jpg");
ofstream fout("test.jpg");//for testing purpose, to see if the string is a right copy
ostringstream ostrm;
unsigned char tmp;
int count = 0;
while ( fin >> tmp ) {
++count;//for testing purpose
ostrm << tmp;
}
string data( ostrm.str() );
cout << count << endl;//ouput 60! Definitely not the right size
fout << string;//only 60 bytes
return 0;
}
Why it stops at 60? It's a strange character at 60, and what should I do to read the jpg to a string?
为什么它停在60?这是60的奇怪字符,我应该怎么做才能将jpg读取为字符串?
UPDATE
更新
Almost there, but after using the suggested method, when I rewrite the string to the output file, it distorted. Find out that I should also specify that the ofstream is in binary mode by ofstream::binary
. Done!
差不多了,但是在使用建议的方法后,当我将字符串重写到输出文件时,它会失真。发现我还应该指定 ofstream 为二进制模式ofstream::binary
。完毕!
By the way what's the difference between ifstream::binary
& ios::binary
, is there any abbreviation for ofstream::binary
?
顺便说一下ifstream::binary
& 和ios::binary
有什么区别,有没有缩写ofstream::binary
?
回答by Benjamin Lindley
Open the file in binary mode, otherwise it will have funny behavior, and it will handle certain non-text characters in inappropriate ways, at least on Windows.
以二进制模式打开文件,否则它会产生有趣的行为,并且它会以不适当的方式处理某些非文本字符,至少在 Windows 上是这样。
ifstream fin("cloud.jpg", ios::binary);
Also, instead of a while loop, you can just read the whole file in one shot:
此外,您可以一次性读取整个文件,而不是 while 循环:
ostrm << fin.rdbuf();
回答by Borgleader
You shouldn't read the file to a string because it is legal for a jpg to contain values that are 0. However in a string, the value 0 has a special meaning (it's the end of string indicator aka \0). You should instead read the file into a vector. You can do this easily like so:
您不应该将文件读取为字符串,因为 jpg 包含 0 值是合法的。但是在字符串中,值 0 具有特殊含义(它是字符串指示符的结尾,也就是 \0)。您应该将文件读入向量。您可以像这样轻松地做到这一点:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc, char* argv[])
{
std::ifstream ifs("C:\Users\Borgleader\Documents\Rapptz.h");
if(!ifs)
{
return -1;
}
std::vector<char> data = std::vector<char>(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
//If you really need it in a string you can initialize it the same way as the vector
std::string data2 = std::string(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>());
std::for_each(data.begin(), data.end(), [](char c) { std::cout << c; });
std::cin.get();
return 0;
}
回答by Jerry Coffin
Try opening the file in binary mode:
尝试以二进制模式打开文件:
ifstream fin("cloud.jpg", std::ios::binary);
At a guess, you were probably trying to read the file on Windows and the 61stcharacter was probably 0x26 -- a control-Z, which (on Windows) will be treated as marking the end of the file.
在猜测,你很可能试图读取Windows中的文件和61日字符可能是0×26 -控制-Z,它(在Windows上)会被当作标记文件的末尾。
As far as how to best do the reading, you end up with a choice between simplicity and speed, as demonstrated in a previous answer.
至于如何最好地进行阅读,您最终会在简单性和速度之间做出选择,如之前的答案所示。