包含头文件
#include <iostream>
#include <fstream>
string strFile = "D:\\pic\\1.jpg"; // 本地图片位置
ifstream in(strFile, ios::in | ios::binary | ios::ate); // 将本地图片读取到输入流
unsigned int size = in.tellg(); // 计算读取的图片大小
char *buffer = new char[size + 1]; // 开辟内存
if (size > 0)
{
in.seekg(0, ios::beg);
in.read(buffer, size); // 将图片二进制数据放到buffer中
buffer[size] = 0;
}
in.close();
ofstream out("D:\\pic\\2.jpg", ios::out | ios::binary | ios::app); // 将准备写入到硬盘的图片文件命名为2.jpg
out.write(buffer, size); // 将图片写入磁盘
out.close(); // 关闭描述符
// 下面将buffer转换成string ,再将string转换成buffer,再次写入磁盘
string strPic((char*)buffer, size);
int uLen = strPic.size();
char * strc = new char[uLen + 1];
memcpy(strc, strPic.c_str(), uLen);
ofstream out1("D:\\pic\\128.jpg", ios::out | ios::binary | ios::app);
out1.write(strc, uLen);
out1.close();