#include "string"
#include "iostream"
#include "fstream"
using namespace std;
#define MAX 20480
void main()
{
string sPicPath = "E:\\10kb.jpg";
string sSavePath = "E:\\Binary.bat";
string sGetPic = "E:\\newpicture.png";
ifstream fin(sPicPath.c_str(), ios::binary);
if(!fin)
{
cout<<"can't open "<<sPicPath<<endl;
return;
}
fin.seekg(,ios::end);//reset FilePtr Position as the end
int ByteLen = fin.tellg();//get file length(bytes)
cout<<"the file length : "<<ByteLen<<" Bytes"<<endl;
fin.seekg(,ios::beg);//restore saved pos
char pBuffer[MAX] = {};
fin.read(pBuffer, sizeof(pBuffer));
fin.close();
ofstream fout(sSavePath.c_str(), ios::binary);
if(!fout)
return;
fout.write(pBuffer,sizeof(pBuffer));
fout.close();
ifstream fins(sSavePath.c_str(), ios::binary);
if(!fins)
{
cout<<"can't open "<<sSavePath<<endl;
return;
}
memset(pBuffer,,sizeof(pBuffer));
fins.read(pBuffer, sizeof(pBuffer));
fins.close();
ofstream fouts(sGetPic.c_str(), ios::binary);
if(!fouts)
return;
fouts.write(pBuffer, sizeof(pBuffer));
cout<<"new file path: "<<sGetPic<<endl;
fouts.close();
}
若您觉得对您有帮助,不妨点个赞