C/C++. Read file to the buffer

#include <iostream>
#include <fstream>

using namespace std;

void main(void) {
   
    int length;
    char* buffer;
   
    ifstream myfile;
    myfile.open("C:\\test.txt", ios::binary);
   
    //get length of file
    myfile.seekg(0, ios::end);
    length = myfile.tellg();
    myfile.seekg(0, ios::beg);
   
    //allocate memory
    buffer = new char[length];
   
    //read data as a block to buffer
    myfile.read(buffer, length);
    myfile.close();
   
    cout.write(buffer, length);
   
    delete[] buffer;
}

Особая конструкция для определения размера файла (в байтах):

int length;
long sizeFile, begin, end;
   
// to write all recived data from buffer in buffer.txt
ifstream toFile("buffer.txt");

// size of buffer.txt;
begin = toFile.tellg();
toFile.seekg(0, ios::end);
end = toFile.tellg();
sizeFile = (end-begin);

printf("buffer.txt size is %d bytes\n", sizeFile);