直接上代码
#include<iostream>
#include<winsock.h>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int picturenumber = 1; //图像计数
int i=0, j=0,length=0; //计算图像长度
string FileDirect = "G:\\usb conn Cpp\\usb"; //图像文件地址
string PictureDirect = FileDirect +"\\" +std::to_string(picturenumber)+".jpg";//图像绝对地址
ifstream in(PictureDirect.c_str(), ios::in|ios::binary); //打开文件,供读取以二进制方式
i = in.tellg(); //当前get流指针的位置
in.seekg(0, ios::end); //基地址为文件结束处,偏移地址为0,于是指针定位在文件结束处
j = in.tellg(); //再次获取当前get流指针的位置
length = j - i; //计算得到图像大小
char * buffer = new char[length];
in.read(buffer, length); //读取图像数据到buffer中
if (buffer == NULL)
{
cout << "读取失败";
}
else
{
cout << "读取成功";
}
}
参考网址:https://www.cnblogs.com/codingmengmeng/p/5545042.html