Skip to content

Instantly share code, notes, and snippets.

@namarium
Created March 28, 2020 12:23
Show Gist options
  • Save namarium/c74178806f68bb9cc3389075395c0cb2 to your computer and use it in GitHub Desktop.
Save namarium/c74178806f68bb9cc3389075395c0cb2 to your computer and use it in GitHub Desktop.
ファイルからバッファー作成(高速版)
#include <cstdio>
#include <vector>
/// <summary>
/// バッファー作成
/// </summary>
/// <param name="path">ファイルのパス</param>
/// <returns>バッファー(失敗時は、空)</returns>
std::vector<unsigned char> MakeBufferFromFile(const wchar_t* path)
{
FILE *fileptr;
if (_wfopen_s(&fileptr, path, L"rb") != 0) {
return{};
}
fseek(fileptr, 0, SEEK_END);
std::vector<unsigned char> buffer(ftell(fileptr));
rewind(fileptr);
fread(buffer.data(), buffer.size(), 1, fileptr);
fclose(fileptr);
return buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment