我需要将jpg文件读取为字符串.我想将此文件上传到我们的服务器,我只是发现API需要一个字符串作为此图片的数据.我按照前一个问题提出建议,我问了Upload pics to a server using 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;
}
为什么它停在60? 60岁时这是一个奇怪的角色,我该怎么做才能将jpg读成一个字符串?
UPDATE
几乎在那里,但在使用建议的方法后,当我将字符串重写为输出文件时,它会失真.发现我还应该通过ofstream :: binary指定ofstream处于二进制模式.完成!
那么ifstream :: binary和amp;之间的区别是什么? ios :: binary,是什么缩写forstream :: binary?