如何读取图像文件,它(的std ::字符串)存储在内存中的C ++? [英] how to read image files and store it in memory(std::string) in c++?

查看:171
本文介绍了如何读取图像文件,它(的std ::字符串)存储在内存中的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了网上的大多数今天却找不到所以我转向StackOverflow的一些建议的答复。

i've researched online for most of today but could not find the answer so i'm turning to stackoverflow for some suggestion.

基本上,我有一个使用卷曲执行PUT方法上载图像文件的C ++库。现在这个库需要一个std ::字符串数据。我有图像文件(如JPG,GIF,PNG)我的本地磁盘上。

basically, i have a c++ library that uses curl to perform PUT method to upload an image file. now this library takes a std::string for data. i have image files (like jpg, gif, png) on my local disk.

我不关心文件的内容(如,我不对它做任何事,除了它传递给这个库PUT方法)。我怎样才能读取图像文件并将其保存在的std ::字符串?如果文件内容包含NULL终止?

i don't care about contents of the file (as in, i don't do anything with it besides passing it to this library for PUT method). how can i read image files and store it in std::string? what if the file content contains NULL terminator?

我尝试添加一些codeS我试过,但我是新来的,我不知道如何粘贴在这里我的code以正确的格式。任何帮助将是AP preciated。

i tried to add some of codes i've tried, but i'm new here and i'm not sure how to paste my code here in the right format. any help would be appreciated.

推荐答案

我能想到的最简单的方法。以二进制方式打开该文件,然后读取整个事情变成字符串流

Easiest method I can think of. Open the file in binary mode, then read the whole thing into a stringstream.

std::ifstream fin("foo.png", std::ios::in | std::ios::binary);
std::ostringstream oss;
oss << fin.rdbuf();
std::string data(oss.str());

这篇关于如何读取图像文件,它(的std ::字符串)存储在内存中的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆