#include <fstream> // ifstream, ifstream::in
using namespace std;

int main(){
	// 1. 打开图片文件
	// 评论区的 @霍鑫网络 帮忙发现一个隐藏的bug,在此表示感谢,已经修正
	ifstream is("test.jpg", ifstream::in | ios::binary);
	// 2. 计算图片长度
	is.seekg(0, is.end);
	int length = is.tellg();
	is.seekg(0, is.beg);
	// 3. 创建内存缓存区
	char * buffer = new char[length];
	// 4. 读取图片
	is.read(buffer, length);
	// 到此,图片已经成功的被读取到内存(buffer)中
    //https://www.coder.work/article/1711506
    //https://stackoverrun.com/cn/q/8220469
	delete [] buffer;
	return 0;
}
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <io.h>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
	string strpath = "D:\\Dtest5\\Readjpg\\1.jpg";
	string strR1 = "D:\\Dtest5\\Readjpg\\10.jpg";
 
	std::ifstream fin(strpath.c_str(), std::ios::binary);
	fin.seekg(0, ios::end);
	int iSize = fin.tellg();
	char* szBuf = new (std::nothrow) char[iSize];
 
	fin.seekg(0, ios::beg);
	fin.read(szBuf, sizeof(char) * iSize);
	fin.close();
 
	std::ofstream fout(strR1.c_str(), std::ios::binary);
	fout.write(szBuf, sizeof(char) * iSize);
	fout.close();
 
	return 0;
}