Qual é a abordagem padrão idiomática do C ++ 17 para ler arquivos binários?

Normalmente, eu usaria apenas E / S de arquivo de estilo C, mas estou tentando uma abordagem moderna de C ++, incluindo os recursos específicos do C ++ 17std::byte estd::filesystem.

Lendo um arquivo inteiro na memória, método tradicional:

#include <stdio.h>
#include <stdlib.h>

char *readFileData(char *path)
{
    FILE *f;
    struct stat fs;
    char *buf;

    stat(path, &fs);
    buf = (char *)malloc(fs.st_size);

    f = fopen(path, "rb");
    fread(buf, fs.st_size, 1, f);
    fclose(f);

    return buf;
}

Lendo um arquivo inteiro na memória, abordagem moderna:

#include <filesystem>
#include <fstream>
#include <string>
using namespace std;
using namespace std::filesystem;

auto readFileData(string path)
{
    auto fileSize = file_size(path);
    auto buf = make_unique<byte[]>(fileSize);
    basic_ifstream<byte> ifs(path, ios::binary);
    ifs.read(buf.get(), fileSize);
    return buf;
}

Isso parece certo? Isso pode ser melhorado?

questionAnswers(1)

yourAnswerToTheQuestion