如何将 C++ 二进制文件读入缓冲区(C++17 方式)
read_file.cpp
#include <iostream>
#include <fstream>
#include <filesystem>
// 获取文件大小以知道需要分配多少内存
std::uintmax_t filesize = std::filesystem::file_size("C037B221110.bin");
// 分配缓冲区以保存文件
char* buf = new char[filesize];
// 读取文件
std::ifstream fin("C037B221110.bin", std::ios::binary);
fin.read(buf, filesize);
if(!fin) {
std::cerr << "Error reading file, could only read " << fin.gcount() << " bytes" << std::endl;
}
// 关闭文件
fin.close();Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow