C++ 을 이용하여 이미지 파일을 한번에 읽어서 복사하는 예
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
cout << "바이너리 파일 다루기" << endl;
//파일의 크기를 미리 확인하여 한번에 읽어서 다른 파일에 복사하는 예
ifstream in;
in.open("D:\\test\\mountains.jpg", ios::binary);
// 파일의 전체 크기를 확인한다
in.seekg(0, ios::end);
int length = in.tellg();
cout << "읽어올 파일의 전체 크기:" << length <<" 바이트"<< endl;
// 파일의 전체 크기만큼 메모리에 로드한다
in.seekg(0, ios::beg);
char* buf = new char[length];
in.read(buf, length);
in.close();
// 메모리에 저장된 파일 데이터를 다른 파일에 저장한다
ofstream out;
out.open("D:\\test\\copy.jpg", ios::binary);
out.write(buf, length);
out.close();
cout << "파일 복사 성공(D:\\test\\copy.jpg)" << endl;
return 0;
}